Wednesday, March 28, 2012
How to enter this command line sql statement...??
I am not sure if I use the ">" , "[", etc... or if I leave them out. Can some one show me exactly how the finished code is supposed to look (in laymans)...
The program I am using to query the DB is called ODBCVIEW. Here is a link to the page with the syntax(located at the bottom) - http://www.slik.co.nz/HTML_help/odbc_view.htm
...and I have also pasted it below:
-COPIED FROM WEBSITE:
ODBCView also supports a non-interactive command line mode to execute a query and save the results to a text file. The syntax is as follows:
ODBCView.exe <DSN=DataSourceName;UID=User;PWD=Password> [<SQLScript.sql>] [<OutputFile.htm|csv|txt>]
Where:
DataSource
The desired datasource name.
UID
An optional user name to logon to the database
PWD
An optional user password
SQLScript
The query to execute.
OutputFile
The path to the output file. The files extension determines the format. Use htm or html to HTML, csv for CSV.If you are only trying to query a database and write the results to a file, better try WinSQL (http://www.download.com/3001-10255_4-10213451.html). ;)|||:D Just what I was looking for!
I'm installing it now - Does it have a 'command-line' mode?
Monday, March 26, 2012
how to encrypt and decypt as a IUSR with read and write only rights
I created an asymmetric key for encryption by password. I am not using a master key because I want to keep the password seperately on the web server, so a hacker cannot get access to both if database gets hacked.
These are the steps I took when I logged in to SQL server management studio using windows authentication:
CREATE ASYMMETRIC KEY ccnumber WITH ALGORITHM = RSA_512
ENCRYPTION BY PASSWORD = 'password';
INSERT INTO Payments (CreditCardNumber,enc_CreditCardNumber)
values( '458724124',
EncryptByAsymKey(AsymKey_ID('ccnumber'), '458724124') )
SELECT CONVERT(varchar(50), DecryptByAsymKey( AsymKey_Id('ccnumber'), enc_CreditCardNumber, N'password' ))
AS Creditcardnumber , Creditcardnumber
FROM payments where Creditcardnumber = '458724124'
When I use the above select statement it works if I make the user a db_owner but I get null if the user is just db_reader and db_writer.
Is there a way to do encryption without making the user a db_owner?
Yes, you can encrypt without being a db_owner. Note that you should not use asymmetric key encryption for encrypting data. You should use symmetric keys to encrypt data and asymmetric keys to protect other keys or for signing code.
To encrypt and decrypt with an asymmetric key, you need to grant CONTROL permission on the key (VIEW is sufficient for encryption, but for decryption you need CONTROL and knowledge of the password).
For encryption and decryption with a symmetric key, the user must be able to open the symmetric key (see permissions section of this BOL article: http://msdn2.microsoft.com/en-us/library/ms190499.aspx). You may also find the following blog post useful: http://blogs.msdn.com/lcris/archive/2006/01/13/512829.aspx.
Thanks
Laurentiu
Wednesday, March 7, 2012
How to document the reports
documentation about a report there, and be happy. But is there a more
structured way of doing it? Some application or plugin for Visual Studio
perhaps?
I'd like it to make a list of what reports are in my report solution, and
shared datasources. And then for each report, a list of datasources used,
maybe a list of datasets and a field for free text comments about "This
report uses a matrix, which displays subtotals on columns and rows."
Which in time can be searched through, so the next time I want a report with
a matrix with subtotals, I can check this one out for any clever tricks I
did to make it work. :)
Kaisa M. LindahlIf you willing to write some code, then it is possible.
Just parse your RDL file(s), analyze it and generate a doc.
"Kaisa M. Lindahl" <kaisaml@.hotmail.com> wrote in message
news:uq9plHUEGHA.2292@.tk2msftngp13.phx.gbl...
> OK, I guess you could always just whip up MS Word and write some
> documentation about a report there, and be happy. But is there a more
> structured way of doing it? Some application or plugin for Visual Studio
> perhaps?
> I'd like it to make a list of what reports are in my report solution, and
> shared datasources. And then for each report, a list of datasources used,
> maybe a list of datasets and a field for free text comments about "This
> report uses a matrix, which displays subtotals on columns and rows."
> Which in time can be searched through, so the next time I want a report
> with a matrix with subtotals, I can check this one out for any clever
> tricks I did to make it work. :)
> Kaisa M. Lindahl
>
How to do this in a report?
CREATE TABLE [dbo].[Sales_Customer_List] (
[cust_no] [char] (10) NOT NULL ,
[cust_name] [char] (35) NULL ,
[distr_channel] [char] (2) NOT NULL ,
[sold_to_sales_grp] [char] (3) NULL ,
[ship_to_sales_grp] [char] (3) NULL ,
[sold_to_sales_rep_cd] [char] (10) NULL ,
[ship_to_sales_rep_cd] [char] (10) NULL ,
[sold_to_sales_rep] [varchar] (30) NULL ,
[ship_to_sales_rep] [varchar] (30) NULL ,
[csr] [char] (35) NOT NULL ,
[csr_email] [char] (60) NOT NULL ,
[credit_mgr] [char] (35) NOT NULL ,
[sales_region] [char] (20) NULL ,
[BusArea01] [decimal](18, 2) NULL ,
[BusArea02] [decimal](18, 2) NULL ,
[BusArea03] [decimal](18, 2) NULL
) ON [PRIMARY]
--GO
and gives me a listing by customer number, customer name,
sold_to_sales_grp, etc thru the Sales region field.
The data itself can have multiple distr_channel values but the other
fields (excluding the busarea01, 02 and 03 fields) will not change
between customers.
The data would be like:
custno = 1234
custname= Customer1
distr_channel = DS
etc etc etc and the BusArea fields would be
BusArea01 = 0
BusArea02 = 137
BusArea03 = 984
A second record would be:
custno = 1234
custname= Customer1
distr_channel = GM
etc etc etc and the BusArea fields would be
BusArea01 = 855
BusArea02 = 0
BusArea03 = 211
A Third record would be:
custno = 6543
Custname = Customer2
distr_channel = CH
etc etc etc and the BusArea fields would be
BusArea01 = 1250
BusArea02 = 0
BusArea03 = 335
A fourth record would be
Custno = 8998
Custname = Customer3
distr_channel = DL
etc etc etc and the BusArea fields would be
BusArea01 = 25000
BusArea02 = 0
BusArea03 = 550
A fifth record would be
Custno = 8998
Custname = Customer3
distr_channel = WA
etc etc etc and the BusArea fields would be
BusArea01 = 0
BusArea02 = 15000
BusArea03 = 0
The line I need to have on my report would be:
C No C Name BA01 BA02 BA03
1234 Customer1 etc etc etc GM 855 DS 137 DS & GM 211 + 984
6543 Customer2 etc etc etc CH 1250 CH 0 CH 335
8998 Customer3 etc etc etc DL 25000 WA 15000 DL 550
Some customers would have 1 Distr_channel, some 4 or 5.
How would you do this in SQL?
Thanks,
SCBetter if you do this in the client app / reporting tool and not in sql serv
er.
HOW TO: Rotate a Table in SQL Server
http://support.microsoft.com/defaul...574&Product=sql
Dynamic Crosstab Queries
http://www.windowsitpro.com/SQLServ...5608/15608.html
Dynamic Cross-Tabs/Pivot Tables
http://www.sqlteam.com/item.asp?ItemID=2955
AMB
"Blasting Cap" wrote:
> I have to write a report in SQL that takes the following data structure:
> CREATE TABLE [dbo].[Sales_Customer_List] (
> [cust_no] [char] (10) NOT NULL ,
> [cust_name] [char] (35) NULL ,
> [distr_channel] [char] (2) NOT NULL ,
> [sold_to_sales_grp] [char] (3) NULL ,
> [ship_to_sales_grp] [char] (3) NULL ,
> [sold_to_sales_rep_cd] [char] (10) NULL ,
> [ship_to_sales_rep_cd] [char] (10) NULL ,
> [sold_to_sales_rep] [varchar] (30) NULL ,
> [ship_to_sales_rep] [varchar] (30) NULL ,
> [csr] [char] (35) NOT NULL ,
> [csr_email] [char] (60) NOT NULL ,
> [credit_mgr] [char] (35) NOT NULL ,
> [sales_region] [char] (20) NULL ,
> [BusArea01] [decimal](18, 2) NULL ,
> [BusArea02] [decimal](18, 2) NULL ,
> [BusArea03] [decimal](18, 2) NULL
> ) ON [PRIMARY]
> --GO
> and gives me a listing by customer number, customer name,
> sold_to_sales_grp, etc thru the Sales region field.
> The data itself can have multiple distr_channel values but the other
> fields (excluding the busarea01, 02 and 03 fields) will not change
> between customers.
> The data would be like:
> custno = 1234
> custname= Customer1
> distr_channel = DS
> etc etc etc and the BusArea fields would be
> BusArea01 = 0
> BusArea02 = 137
> BusArea03 = 984
> A second record would be:
> custno = 1234
> custname= Customer1
> distr_channel = GM
> etc etc etc and the BusArea fields would be
> BusArea01 = 855
> BusArea02 = 0
> BusArea03 = 211
> A Third record would be:
> custno = 6543
> Custname = Customer2
> distr_channel = CH
> etc etc etc and the BusArea fields would be
> BusArea01 = 1250
> BusArea02 = 0
> BusArea03 = 335
> A fourth record would be
> Custno = 8998
> Custname = Customer3
> distr_channel = DL
> etc etc etc and the BusArea fields would be
> BusArea01 = 25000
> BusArea02 = 0
> BusArea03 = 550
> A fifth record would be
> Custno = 8998
> Custname = Customer3
> distr_channel = WA
> etc etc etc and the BusArea fields would be
> BusArea01 = 0
> BusArea02 = 15000
> BusArea03 = 0
>
> The line I need to have on my report would be:
> C No C Name BA01 BA02 BA03
> 1234 Customer1 etc etc etc GM 855 DS 137 DS & GM 211 + 984
> 6543 Customer2 etc etc etc CH 1250 CH 0 CH 335
> 8998 Customer3 etc etc etc DL 25000 WA 15000 DL 550
> Some customers would have 1 Distr_channel, some 4 or 5.
> How would you do this in SQL?
>
> Thanks,
> SC
>
Sunday, February 19, 2012
How To Do An Average Of A Count
We have an order processing database which includes the standard Order
Header/Order Lines tables. I'm trying to write a query to get the average
number of lines per order over a given period, and I'm stuck :-(
I can get the number of lines per order with:
SELECT COUNT(*) FROM OrderDetails
INNER JOIN Order Header ON
OrderHeader.OrderNo = OrderDetails.OrderNo
WHERE OrderHeader.OrderDate .... {various criteria} ...
GROUP BY OrderDetails.OrderNumber
But how do I then get an average of this for the period?
TIA
Mike BannonOn Fri, 5 Mar 2004 10:43:46 +0000 (UTC), "Mike Bannon"
<mikeb@.dataformation.co.uk> wrote:
>Hi All
>We have an order processing database which includes the standard Order
>Header/Order Lines tables. I'm trying to write a query to get the average
>number of lines per order over a given period, and I'm stuck :-(
>I can get the number of lines per order with:
>SELECT COUNT(*) FROM OrderDetails
>INNER JOIN Order Header ON
>OrderHeader.OrderNo = OrderDetails.OrderNo
>WHERE OrderHeader.OrderDate .... {various criteria} ...
>GROUP BY OrderDetails.OrderNumber
>But how do I then get an average of this for the period?
>TIA
>Mike Bannon
Use a derived table. A derived table is a complete SELECT statement in
parentheses and followed by an alias name that is placed in the FROM clause of
another query. In your case, it might be something like this...
SELECT OrderStats.OrderYear, OrderStats.OrderQtr, AVG(OrderStats.OrderLines)
FROM (
SELECT
OrderDetails.OrderYear,
OrderDetails.OrderQtr,
OrderLines = COUNT(*)
FROM OrderDetails
INNER JOIN Order Header ON
OrderHeader.OrderNo = OrderDetails.OrderNo
WHERE OrderHeader.OrderDate .... {various criteria} ...
GROUP BY
OrderDetails.OrderYear,
OrderDetails.OrderQtr,
OrderDetails.OrderNumber
) OrderStats
GROUP BY
OrderStats.OrderYear,
OrderStats.OrderQtr,|||"Steve Jorgensen" <nospam@.nospam.nospam> wrote in message
news:t9ng40puunmh9bkf58iffbi6k53qggabno@.4ax.com...
> On Fri, 5 Mar 2004 10:43:46 +0000 (UTC), "Mike Bannon"
> <mikeb@.dataformation.co.uk> wrote:
> >Hi All
> >We have an order processing database which includes the standard Order
> >Header/Order Lines tables. I'm trying to write a query to get the average
> >number of lines per order over a given period, and I'm stuck :-(
> >I can get the number of lines per order with:
> >SELECT COUNT(*) FROM OrderDetails
> >INNER JOIN Order Header ON
> >OrderHeader.OrderNo = OrderDetails.OrderNo
> >WHERE OrderHeader.OrderDate .... {various criteria} ...
> >GROUP BY OrderDetails.OrderNumber
> >But how do I then get an average of this for the period?
> >TIA
> >Mike Bannon
>
> Use a derived table. A derived table is a complete SELECT statement in
> parentheses and followed by an alias name that is placed in the FROM
clause of
> another query. In your case, it might be something like this...
> SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
AVG(OrderStats.OrderLines)
> FROM (
> SELECT
> OrderDetails.OrderYear,
> OrderDetails.OrderQtr,
> OrderLines = COUNT(*)
> FROM OrderDetails
> INNER JOIN Order Header ON
> OrderHeader.OrderNo = OrderDetails.OrderNo
> WHERE OrderHeader.OrderDate .... {various criteria} ...
> GROUP BY
> OrderDetails.OrderYear,
> OrderDetails.OrderQtr,
> OrderDetails.OrderNumber
> ) OrderStats
> GROUP BY
> OrderStats.OrderYear,
> OrderStats.OrderQtr,
Thanks, Steve, works a treat :-)
Mike|||>> We have an order processing database which includes the standard
Order
Header/Order Lines tables. <<
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. My guess is that the order details looks like this:
CREATE TABLE OrderDetails
(order_nbr INTEGER NOT NULL
REFERENCES Orders(order_nbr)
ON UPDATE CASCADE
ON DELETE CASCADE,
upc CHAR(10) NOT NULL -- or other industry code
REFERENCES Inventory(upc)
ON UPDATE CASCADE
ON DELETE CASCADE,
qty INTEGER NOT NULL,
...);
>> I'm trying to write a query to get the average number of lines per
order over a given period, and I'm stuck :-( <<
I hope that you mean the average number of **items** per order over a
given period. The way you said this would imply that you are copying
the input form (where lines are PHYSICAL), and have no proper LOGICAL
model. If I order my eggs one at a time, I get 12 lines; If I map the
order into a LOGICAL row, I get one row with a quantity of 12 eggs
(and then I can look for packaging units, discounts, etc.)
SELECT AVG(X.tally) AS avg_per_order
FROM (SELECT COUNT(*)
FROM OrderDetails AS D1
WHERE D1.order_nbr
IN (SELECT O1.order_nbr
FROM Orders AS O1
WHERE O1.order_dt
BETWEEN @.my_start_dateAND @.my_end_date)
GROUP BY order_nbr) AS X(tally);
The trick for aggregates inside aggregates is to use a derived table
for the lower level aggregations, then nest them outward.|||Steve Jorgensen (nospam@.nospam.nospam) writes:
> Use a derived table. A derived table is a complete SELECT statement in
> parentheses and followed by an alias name that is placed in the FROM
> clause of another query. In your case, it might be something like
> this...
> SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
> AVG(OrderStats.OrderLines)
In case you want an average with a fraction, permit me to suggest an
improvement:
SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
AVG(1.0 * OrderStats.OrderLines)
Without 1.0 you will get an integer result.
--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||On Sun, 7 Mar 2004 22:26:56 +0000 (UTC), Erland Sommarskog <sommar@.algonet.se>
wrote:
>Steve Jorgensen (nospam@.nospam.nospam) writes:
>> Use a derived table. A derived table is a complete SELECT statement in
>> parentheses and followed by an alias name that is placed in the FROM
>> clause of another query. In your case, it might be something like
>> this...
>>
>> SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
>> AVG(OrderStats.OrderLines)
>In case you want an average with a fraction, permit me to suggest an
>improvement:
> SELECT OrderStats.OrderYear, OrderStats.OrderQtr,
> AVG(1.0 * OrderStats.OrderLines)
>Without 1.0 you will get an integer result.
Thanks for correcting my oversight.