Showing posts with label select. Show all posts
Showing posts with label select. Show all posts

Friday, March 30, 2012

No Lock

Lets say i have a view.

vuTestingNoLocks

this view looks like this...

Create View vuTestingNoLocks as

SElect *

From dbo.Employees

inner Join dbo.EmployeeTerritories on EmployeeTerritories.EmployeeID = Employees.EmployeeID

If I select from this view using Select * From vuTestingNoLocks (NOLOCK)

Does the (nolock) command propegate down through the tables? Meaning will it scan tables that are locked still ignoring their locks?

I think you should be able to test it out and come to your own conclusions. Let me know what you are going to interpret from this.

In one window, create these tables, populate dummy data and create the view

Code Snippet

--Start of window1

create table t1 (t1id int identity(10,10),

t1code char(2)

)

create table t2 (t2id int,

t2name varchar(10)

)

insert t1 select 'AB'

insert t1 select 'BC'

insert t1 select 'CD'

insert t2 select 10, 'Group AB'

insert t2 select 20, 'Group BC'

insert t2 select 30, 'Group CD'

create view vw_t1_t2 as

select t1id, t1code, t2name

from t1 join t2

on t1.t1id = t2id

--End of window1

--start of window2

--we are setting up to take exclusive lock

begin tran

update vw_t1_t2

set t1code = 'KK'

--end of window2

--start of window3

--Issue this once

select * from vw_t1_t2 (nolock)

--and this later

select * from vw_t1_t2

--end of window3

|||

Yes, NOLOCK table hint will be propageted to the tables in view defination.

It is also very usefull for indexed views as they are treated as tables as well.

Please see following article for more detail.

http://msdn2.microsoft.com/en-us/library/ms190237.aspx

Hope this will answer your question.

Thanks,

FB

|||

I appeciate you taking the time to help me test the theory myself. I didn't think of Opening a transaction and not comitting it to Lock the table..

To answer your question it does propogate down..

|||

THank you for that answer it was very helpful...

No Lock

Lets say i have a view.

vuTestingNoLocks

this view looks like this...

Create View vuTestingNoLocks as

SElect *

From dbo.Employees

inner Join dbo.EmployeeTerritories on EmployeeTerritories.EmployeeID = Employees.EmployeeID

If I select from this view using Select * From vuTestingNoLocks (NOLOCK)

Does the (nolock) command propegate down through the tables? Meaning will it scan tables that are locked still ignoring their locks?

I think you should be able to test it out and come to your own conclusions. Let me know what you are going to interpret from this.

In one window, create these tables, populate dummy data and create the view

Code Snippet

--Start of window1

create table t1 (t1id int identity(10,10),

t1code char(2)

)

create table t2 (t2id int,

t2name varchar(10)

)

insert t1 select 'AB'

insert t1 select 'BC'

insert t1 select 'CD'

insert t2 select 10, 'Group AB'

insert t2 select 20, 'Group BC'

insert t2 select 30, 'Group CD'

create view vw_t1_t2 as

select t1id, t1code, t2name

from t1 join t2

on t1.t1id = t2id

--End of window1

--start of window2

--we are setting up to take exclusive lock

begin tran

update vw_t1_t2

set t1code = 'KK'

--end of window2

--start of window3

--Issue this once

select * from vw_t1_t2 (nolock)

--and this later

select * from vw_t1_t2

--end of window3

|||

Yes, NOLOCK table hint will be propageted to the tables in view defination.

It is also very usefull for indexed views as they are treated as tables as well.

Please see following article for more detail.

http://msdn2.microsoft.com/en-us/library/ms190237.aspx

Hope this will answer your question.

Thanks,

FB

|||

I appeciate you taking the time to help me test the theory myself. I didn't think of Opening a transaction and not comitting it to Lock the table..

To answer your question it does propogate down..

|||

THank you for that answer it was very helpful...

Wednesday, March 28, 2012

No ISSQLROLE column in sysusers?

The query below complains of "Invalid column name 'ISSQLROLE'." when run on
installation that uses the Turkish character set.
SELECT * FROM sysusers WHERE NAME = 'BOB' AND ISSQLROLE = 0
I'm confused; why would the column be missing/renamed? Reinstalling SQL Ser
ver to use Latin character set works as always. Any words will be very much
appreciated. (I don't have much experience with character sets outside of t
he one that I use here in California.)
Thanks in advance,
James Hunter RossHi James,
Try the column name in lower case. Turkish has both dotted and non-dotted I'
s, and sometimes it is difficult to tell them apart if you're not used to th
e alphabet. In this case it seems to me that the uppercase I in ISSQLROLE in
interpreted by SQL Server as a non-dotted I, where it should be a dotted i.
--
Jacco Schalkwijk
SQL Server MVP
"James Hunter Ross" <james.ross@.oneilsoft.com> wrote in message news:ujLG32q
CEHA.688@.tk2msftngp13.phx.gbl...
The query below complains of "Invalid column name 'ISSQLROLE'." when run on
installation that uses the Turkish character set.
SELECT * FROM sysusers WHERE NAME = 'BOB' AND ISSQLROLE = 0
I'm confused; why would the column be missing/renamed? Reinstalling SQL Ser
ver to use Latin character set works as always. Any words will be very much
appreciated. (I don't have much experience with character sets outside of t
he one that I use here in California.)
Thanks in advance,
James Hunter Ross|||I change only the "I" in "ISSQLROLE" in our query to "iSSQLROLE" and it work
s! What the...
I'm freaking out. I can't even think. Then, "I" is not actually upper case "
i", we are doomed. We have never been particularly disciplined when writing
queries to match the case of the query to the case of the column/table names
.
I don't suppose there is any easy way to dance around this, is there?
James|||Hi James,
Indeed in the Turkish collation "I" is not upper case "i", it is upper case
"i". Upper case "i" is "I".
I don't think that there is a way around it except a big search and replace
to change all the I's into i's.
The other option involves rebuilding the master database with a
Latin1_General collation (while keeping your own database at Turkish), but
your customer(s) might not be very happy with that, and if you have string
comparisons with columns in temporary tables they will fail most likely.
Jacco Schalkwijk
SQL Server MVP
"James Hunter Ross" <james.ross@.oneilsoft.com> wrote in message
news:uk1xlPrCEHA.3280@.TK2MSFTNGP09.phx.gbl...
I change only the "I" in "ISSQLROLE" in our query to "iSSQLROLE" and it
works! What the...
I'm freaking out. I can't even think. Then, "I" is not actually upper case
"i", we are doomed. We have never been particularly disciplined when writing
queries to match the case of the query to the case of the column/table
names.
I don't suppose there is any easy way to dance around this, is there?
James

No info in diagramed tables

I ran the diagram tool in EM SQL 7.0 and the table appears but nothing else is visible. If you check properties everything is grayed out. Select tables with Joins and all the tables show but again no column names, key or joins visible. I opened the diagram in Northwind and it appears correctly. I must have missed something, hope someone knows what.

Thanks in advance.
KayteeHOriginally posted by Kayteeh
I ran the diagram tool in EM SQL 7.0 and the table appears but nothing else is visible. If you check properties everything is grayed out. Select tables with Joins and all the tables show but again no column names, key or joins visible. I opened the diagram in Northwind and it appears correctly. I must have missed something, hope someone knows what.

Thanks in advance.
KayteeH

Can you right click on the table and get a menu that has an option called table view?|||could it be that you're missing permissions?|||Originally posted by Brett Kaiser
Can you right click on the table and get a menu that has an option called table view?

No - I don't see a menu that says table view.|||Originally posted by ms_sql_dba
could it be that you're missing permissions?

That's what I thought, but I am dbo. I can use the diagram feature in other databases I own. Just not this particular database.

No history information for jobs

Hi All,
When I right click on some jobs in sql server and select view job history, s
ometimes I get "There is no history information for this job". I was able to
see job history few hours/days back. I have not cleared logs nor restarted
the box. But still i am not
able to see any history information. I can however see history information o
n other jobs. Can you tell me why is sql server wiping of job history inform
ation for some jobs. What can i do to prevent it?
Regards,
PROHave you tried increasing the values in SQLAgent-->Properties-->Job
System tab? Look at the Maximum job history log size(rows) and Maximum
job history rows per job values. See if that works for you.
Mark Allison, SQL Server MVP
PRashanth wrote:

> Hi All,
> When I right click on some jobs in sql server and select view job history, sometim
es I get "There is no history information for this job". I was able to see job histo
ry few hours/days back. I have not cleared logs nor restarted the box. But still i a
m n
ot able to see any history information. I can however see history information on other jobs
. Can you tell me why is sql server wiping of job history information for some jobs. What c
an i do to prevent it?
> Regards,
> PRO|||Hi Mark,
Thank you very much for your reply. The current setting of my sql server
is Max. job history log size(rows): 500000
max job history rows per job: 1000
Also, i dont understand why there is no job history for only some of the
jobs
Regards,
PRO
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!|||--
|
| Hi Mark,
|
| Thank you very much for your reply. The current setting of my sql server
| is Max. job history log size(rows): 500000
| max job history rows per job: 1000
|
| Also, i dont understand why there is no job history for only some of the
| jobs
|
| Regards,
| PRO
--
It is possible that the job history of more frequently running jobs are
overwriting those of the less frequently running jobs. To address this,
increase your max job history log size and decrease your max job history
rows per job.
Hope this helps,
Eric Cardenas
SQL Server support

No field delimiters using bcp command

Hi,

I am using a bcp command to load data into a text file . The command is below:

C:\>bcp "select ltrim(rtrim(char25))+replicate ('X',25-len(char25)),CONVERT(varc
har(8),dateg,112) as [yyyymmdd],flag1,replace( replicate ('0',19-len(amount)) +
ltrim(rtrim(amount)),'.',','),replace(replicate ('0',9-len(dperc)) + ltrim(rtrim
(dperc)),'.',',') from Bank_Info.dbo.ddd" queryout c:\xxxx\replicate_replace.tx
t -c -U sax -S KARAFOKAS -C 1252 -P passsax

The command runs fine , the problem is , the output in the text file is with tab delimited form. I want the format NOT to have tab delimited form but the values actually to have a continuation. That is, nothing to split one value from the other.

This is the output with tab delimited format.

vvvXXXXXXXXXXXXXXXXXXXXXX 20071112 h 0000000000005555,70 066,50000
abcXXXXXXXXXXXXXXXXXXXXXX 19000101 y 0454545454523456,45 077,30000
xyzcccXXXXXXXXXXXXXXXXXXX 19000101 x 0000000000003456,00 077,99865
fXXXXXXXXXXXXXXXXXXXXXXXX 20030302 6 0000000000232323,45 005,00000

I want the output to have to tabs , as shown below:

vvvXXXXXXXXXXXXXXXXXXXXXX20071112h0000000000005555 ,70066,50000
abcXXXXXXXXXXXXXXXXXXXXXX19000101y0454545454523456 ,45077,30000
xyzcccXXXXXXXXXXXXXXXXXXX19000101x0000000000003456 ,00077,99865
fXXXXXXXXXXXXXXXXXXXXXXXX2003030260000000000232323 ,45005,00000

Columns values should not be seperated by tabs. Any thoughts?

Thank you
George

Quote:

Originally Posted by karafokas

Hi,

I am using a bcp command to load data into a text file . The command is below:

C:\>bcp "select ltrim(rtrim(char25))+replicate ('X',25-len(char25)),CONVERT(varc
har(8),dateg,112) as [yyyymmdd],flag1,replace( replicate ('0',19-len(amount)) +
ltrim(rtrim(amount)),'.',','),replace(replicate ('0',9-len(dperc)) + ltrim(rtrim
(dperc)),'.',',') from Bank_Info.dbo.ddd" queryout c:\xxxx\replicate_replace.tx
t -c -U sax -S KARAFOKAS -C 1252 -P passsax

The command runs fine , the problem is , the output in the text file is with tab delimited form. I want the format NOT to have tab delimited form but the values actually to have a continuation. That is, nothing to split one value from the other.

This is the output with tab delimited format.

vvvXXXXXXXXXXXXXXXXXXXXXX 20071112 h 0000000000005555,70 066,50000
abcXXXXXXXXXXXXXXXXXXXXXX 19000101 y 0454545454523456,45 077,30000
xyzcccXXXXXXXXXXXXXXXXXXX 19000101 x 0000000000003456,00 077,99865
fXXXXXXXXXXXXXXXXXXXXXXXX 20030302 6 0000000000232323,45 005,00000

I want the output to have to tabs , as shown below:

vvvXXXXXXXXXXXXXXXXXXXXXX20071112h0000000000005555 ,70066,50000
abcXXXXXXXXXXXXXXXXXXXXXX19000101y0454545454523456 ,45077,30000
xyzcccXXXXXXXXXXXXXXXXXXX19000101x0000000000003456 ,00077,99865
fXXXXXXXXXXXXXXXXXXXXXXXX2003030260000000000232323 ,45005,00000

Columns values should not be seperated by tabs. Any thoughts?

Thank you
George


looks like you want a fixed-length output. have you tried passing -t "" ? or something like that? -t is the bcp parameter for field terminator.sql

No execution plan in profiler

I'm trying to track down a problem with a very simple sql insert statement.
INSERT INTO tblRecordingId SELECT 82417, MAX(RecordingId)+1 FROM
tblRecordingId
This statement has recently begun to cause timeouts (but not all the time).
When it does work, I get this plan:
Table Insert(OBJECT:([myroc].[dbo].[tblRecordingId]),
SET:([tblRecordingId].[RecordingId]=RaiseIfNull([Expr1004]),
[tblRecordingId].[AccountId]=RaiseIfNull(82440)))
|--Top(1)
|--Compute Scalar(DEFINE:([Expr1004]=[Expr1002]+1))
|--Stream
Aggregate(DEFINE:([Expr1002]=MAX([tblRec
ordingId].[RecordingId])))
|--Table Scan(OBJECT:([myroc].[dbo].[tblRecordingId]))
CPU=16, Reads=121, Writes=0, Duration=10860.
When it causes a timeout, there's no execution plan in the profiler. CPU=15,
Reads=40, Writes=0, Duration=40000.
tblRecordingId contains 2 int columns and 590 rows. There are no indexes or
keys.
I did add deadlock to the profiler but didn't get any entries. The server is
not doing much other work (cpu usage is very low.)
I also tried it in the query analyzer. The query usually executes in less
than a second, but sometimes it takes more than a minute.
Any ideas on what the problem is or hints on what to search for?
PeterMost probably a locking problem. The subquery that gets the MAX value does a
table scan. Can you add
an index on the RecordingId column?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Peter Laan" <plnews2000@.yahoo.se> wrote in message news:e7S7Uly0FHA.3780@.TK2MSFTNGP12.phx.
gbl...
> I'm trying to track down a problem with a very simple sql insert statement
.
> INSERT INTO tblRecordingId SELECT 82417, MAX(RecordingId)+1 FROM tblRecord
ingId
> This statement has recently begun to cause timeouts (but not all the time)
. When it does work, I
> get this plan:
> Table Insert(OBJECT:([myroc].[dbo].[tblRecordingId]),
> SET:([tblRecordingId].[RecordingId]=RaiseIfNull([Expr1004]),
> [tblRecordingId].[AccountId]=RaiseIfNull(82440)))
> |--Top(1)
> |--Compute Scalar(DEFINE:([Expr1004]=[Expr1002]+1))
> |--Stream Aggregate(DEFINE:([Expr1002]=MAX([tblRec
ordingId].[RecordingId])))
> |--Table Scan(OBJECT:([myroc].[dbo].[tblRecordingId]))
> CPU=16, Reads=121, Writes=0, Duration=10860.
> When it causes a timeout, there's no execution plan in the profiler. CPU=1
5, Reads=40, Writes=0,
> Duration=40000.
> tblRecordingId contains 2 int columns and 590 rows. There are no indexes o
r keys.
> I did add deadlock to the profiler but didn't get any entries. The server
is not doing much other
> work (cpu usage is very low.)
> I also tried it in the query analyzer. The query usually executes in less
than a second, but
> sometimes it takes more than a minute.
>
> Any ideas on what the problem is or hints on what to search for?
>
> Peter
>|||Peter Laan wrote:
> I'm trying to track down a problem with a very simple sql insert
> statement.
> INSERT INTO tblRecordingId SELECT 82417, MAX(RecordingId)+1 FROM
> tblRecordingId
> This statement has recently begun to cause timeouts (but not all the
> time). When it does work, I get this plan:
> Table Insert(OBJECT:([myroc].[dbo].[tblRecordingId]),
> SET:([tblRecordingId].[RecordingId]=RaiseIfNull([Expr1004]),
> [tblRecordingId].[AccountId]=RaiseIfNull(82440)))
> |--Top(1)
> |--Compute Scalar(DEFINE:([Expr1004]=[Expr1002]+1))
> |--Stream
> Aggregate(DEFINE:([Expr1002]=MAX([tblRec
ordingId].[RecordingId])))
> |--Table Scan(OBJECT:([myroc].[dbo].[tblRecordingId]))
> CPU=16, Reads=121, Writes=0, Duration=10860.
> When it causes a timeout, there's no execution plan in the profiler.
> CPU=15, Reads=40, Writes=0, Duration=40000.
> tblRecordingId contains 2 int columns and 590 rows. There are no
> indexes or keys.
> I did add deadlock to the profiler but didn't get any entries. The
> server is not doing much other work (cpu usage is very low.)
> I also tried it in the query analyzer. The query usually executes in
> less than a second, but sometimes it takes more than a minute.
>
> Any ideas on what the problem is or hints on what to search for?
>
> Peter
Tibor is correct. The table scan is the problem. Do you not have an
index on the RecordingID column? If not, you need to add one. Also, are
you using the MAX command to return the next key value for inserting? If
so, you really need a unique index on the column and the statement may
not guarantee unique results. I would recommend that you either use an
identity column to have SQL Server auto-generate the next key value or
you use an another table that holds the next value and update it and
grab the key when needed.
David Gugick
Quest Software
www.imceda.com
www.quest.com|||[posted and mailed, vnligen svara i nys
Peter Laan (plnews2000@.yahoo.se) writes:
> When it causes a timeout, there's no execution plan in the profiler.
> CPU=15, Reads=40, Writes=0, Duration=40000.
This is because the timeout is something that occurs in the client that
causes it to cancel the batch, and the plan event is not generated until
the query completes.
Beware that when you get command timeouts, you must issue something
like "IF @.@.trancount > 0 ROLLBACK TRANSACTION", as any transaction
started by the batch is not rolled back automatically.
Or even better, set the command timeout to 0 which means wait forever.
Does make life simpler.

> tblRecordingId contains 2 int columns and 590 rows. There are no indexes
> or keys.
As Tibor and David said, for this type of query you must have an index.

> I did add deadlock to the profiler but didn't get any entries.
A deadlock is not a timeout. That's when two processes are waiting for
each to other release resources. As Tibor and David said, you probably
had a blocking scenario.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks to all who replied! As you correctly guessed it was a blocking
problem.
Petersql

Monday, March 26, 2012

No distinct in a select into stement ?

Dear MSSQL experts,

I use MSSQL 2000 and encountered a strange problem wqhile I tried to
use a select into statement .

If I perform the command command below I get only one dataset which has
the described properties.
If I use the same statement in a select into statement (see the second
select) I get several datasets with the described properties like I
didn't use distinct
Is there any posiibility to use destinct in a select into statement

select distinct IDENTITY (int) as ID, Title1 as Title1, Title2 as
Title2, Title3 as Title3,
AggregationTitle1 as AggregationTitle1, AggregationTitle2 as
AggregationTitle2,
AggregationTitle3 as AggregationTitle3, AggregationTitle4 as
AggregationTitle4
from Variables where Title1 is not NULL or Title2 is not NULL or
Title3 is not NULL or
AggregationTitle1 is not NULL or AggregationTitle2 is not NULL or
AggregationTitle3 is not NULL or AggregationTitle4 is not NULL;

This is the same with select into :

select distinct IDENTITY (int) as ID, Title1 as Title1, Title2 as
Title2, Title3 as Title3,
AggregationTitle1 as AggregationTitle1, AggregationTitle2 as
AggregationTitle2,
AggregationTitle3 as AggregationTitle3, AggregationTitle4 as
AggregationTitle4
into VarTitles from Variables where Title1 is not NULL or Title2 is
not NULL or Title3 is not NULL or
AggregationTitle1 is not NULL or AggregationTitle2 is not NULL or
AggregationTitle3 is not NULL or
AggregationTitle4 is not NULL;

Hope anyone can help.

Best regards,

Daniel Wetzler

IDaniel Wetzler wrote:
> Dear MSSQL experts,
> I use MSSQL 2000 and encountered a strange problem wqhile I tried to
> use a select into statement .
> If I perform the command command below I get only one dataset which has
> the described properties.
> If I use the same statement in a select into statement (see the second
> select) I get several datasets with the described properties like I
> didn't use distinct
> Is there any posiibility to use destinct in a select into statement
> select distinct IDENTITY (int) as ID, Title1 as Title1, Title2 as
> Title2, Title3 as Title3,
> AggregationTitle1 as AggregationTitle1, AggregationTitle2 as
> AggregationTitle2,
> AggregationTitle3 as AggregationTitle3, AggregationTitle4 as
> AggregationTitle4
> from Variables where Title1 is not NULL or Title2 is not NULL or
> Title3 is not NULL or
> AggregationTitle1 is not NULL or AggregationTitle2 is not NULL or
> AggregationTitle3 is not NULL or AggregationTitle4 is not NULL;
>
> This is the same with select into :
> select distinct IDENTITY (int) as ID, Title1 as Title1, Title2 as
> Title2, Title3 as Title3,
> AggregationTitle1 as AggregationTitle1, AggregationTitle2 as
> AggregationTitle2,
> AggregationTitle3 as AggregationTitle3, AggregationTitle4 as
> AggregationTitle4
> into VarTitles from Variables where Title1 is not NULL or Title2 is
> not NULL or Title3 is not NULL or
> AggregationTitle1 is not NULL or AggregationTitle2 is not NULL or
> AggregationTitle3 is not NULL or
> AggregationTitle4 is not NULL;
> Hope anyone can help.
> Best regards,
> Daniel Wetzler

The IDENTITY function makes each row unique so DISTINCT doesn't
eliminate the duplicates in this case. Interestingly, this behaviour
seems to have changed in SQL Server 2005. If I run your SELECT INTO on
2005 I get a different execution plan with the IDENTITY value computed
after DISTINCT.

For 2000 the workaround is easy. The following should insert just one
row into vartitles.

CREATE TABLE variables (title1 VARCHAR(10) NULL, title2 VARCHAR(10)
NULL, title3 VARCHAR(10) NULL, aggregationtitle1 VARCHAR(10) NULL,
aggregationtitle2 VARCHAR(10) NULL, aggregationtitle3 VARCHAR(10) NULL,
aggregationtitle4 VARCHAR(10) NULL);

INSERT INTO variables VALUES ('1','1','1','1','1','1','1');
INSERT INTO variables VALUES ('1','1','1','1','1','1','1');

SELECT IDENTITY (INT) AS id,
title1, title2, title3, aggregationtitle1, aggregationtitle2,
aggregationtitle3, aggregationtitle4
INTO VarTitles
FROM (
SELECT DISTINCT
title1, title2, title3, aggregationtitle1, aggregationtitle2,
aggregationtitle3, aggregationtitle4
FROM variables
WHERE title1 IS NOT NULL
OR title2 IS NOT NULL
OR title3 IS NOT NULL
OR aggregationtitle1 IS NOT NULL
OR aggregationtitle2 IS NOT NULL
OR aggregationtitle3 IS NOT NULL
OR aggregationtitle4 IS NOT NULL) AS V ;

SELECT * FROM vartitles ;

Hope this helps.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||Thank you very much.
This was a very helpful hint.

Best regards,

Daniel|||Minor trick to make the code easier to read and maintain:

WHERE COALESCE (title1, title2, title3) IS NOT NULL
OR COALESCE (ggregation_title1, aggregation_title2,
aggregation_title3, aggregation_title4) IS NOT NULL

Unfortunately these columns look like repeated and a really bad 1NF
problem. I have the feeling that you might have wanted to use
COALESCE() in the SELECT list to get a non-null title and non-null
aggregation_title instead of this convoluted query.|||> Minor trick to make the code easier to read and maintain:
> WHERE COALESCE (title1, title2, title3) IS NOT NULL
> OR COALESCE (ggregation_title1, aggregation_title2,
> aggregation_title3, aggregation_title4) IS NOT NULL

and... bang goes performance too other than a probable clustered index scan
/ table scan.

Consider these two statements on my 800,000 row 834MByte message table for
the nntp forums...

There is a non-clustered index on nntp_author and there is non-clustered
index on author_id

-- Query 1
select count(*)
from mb_message_detail
where nntp_author is not null
or author_id is not null

-- Query 2
select count(*)
from mb_message_detail
where coalesce( nntp_author, author_id ) is not null

Query 1 will use the index author_id and give a half reasonable plan.
Query 2 will do a clustered index scan

Out of 100%, Query 1 is 12% and Query 2 is a whopping 88%

Seriously, go and get a junior job as a programmer and get some very needed
industrial / real world experience instead of bashing people down on here,
as far as 'newbie' goes - you have room to talk.....

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1145969845.021110.74070@.t31g2000cwb.googlegro ups.com...
> Minor trick to make the code easier to read and maintain:
> WHERE COALESCE (title1, title2, title3) IS NOT NULL
> OR COALESCE (ggregation_title1, aggregation_title2,
> aggregation_title3, aggregation_title4) IS NOT NULL
> Unfortunately these columns look like repeated and a really bad 1NF
> problem. I have the feeling that you might have wanted to use
> COALESCE() in the SELECT list to get a non-null title and non-null
> aggregation_title instead of this convoluted query.|||-------
Seriously, go and get a junior job as a programmer and get some very
needed
industrial / real world experience instead of bashing people down on
here,
as far as 'newbie' goes - you have room to talk.....
-------

<BIG GRIN> from on who has been the receiving side of one of CELKO's
many put-downs.

Friday, March 23, 2012

No Create Table as Select from ?

SQLServer2k doesn't let me do a:
CREATE TABLE myNewTbl
AS
select fld1 from myOldTbl
In oracle it works.
Thanks,
Carlbelow code will do the same job in sql2000

select fld1 into myNewTbl from myOldTbl|||Great, I guess that's not as good as the ahem, the oracle way. Suppose you had a union query. How would that work?|||what union query? u meant something like,

select fld1 into myNewTbl from (select fld1 from table1 union select fld1 from table2) as tm
post ur union query.|||Ya sorry, I should've been more specific.

In oracle I would:

CREATE TABLE myNewTable AS
SELECT fld1 FROM myOldTable1
UNION
SELECT fld2 FROM myOldTable2

Mostly i use this to get a roughed out table structure, then set up my append queries, so your 1st reply did the job, I just used the 1st SELECT.

Thanks,
Carl|||SELECT *
INTO myNewTable
FROM (
SELECT fld1 FROM myOldTable1
UNION
SELECT fld2 FROM myOldTable2) AS XXX|||SELECT *
INTO myNewTable
FROM (
SELECT fld1 FROM myOldTable1
UNION
SELECT fld2 FROM myOldTable2) AS XXX

Why not just:

SELECT fld1
INTO myNewTable
FROM myOldTable1
UNION
select fld2
FROM myOldTable2sql

No columns when using temp tables in T-SQL in OLEDB Source

We have a complicated select query that needs to build a couple temporary work tables that are then used in the final select statement (in an OLEDB Source data flow control). We can click preview and see the resultset, but if we click on the Columns view there are no columns. We can save and close the OLEDB Source control but downstream from it there are messages saying that there are no input columns. The T-SQL looks something like this (abbreviated):

SELECT fieldlist INTO #temp1 FROM table

SELECT fieldlist INTO #temp2 FROM table

SELECT fieldlist FROM table INNER JOIN #temp1 INNER JOIN #temp2

DROP TABLE #temp1; DROP TABLE #temp2

Has anyone been able to use temp tables in a source SQL statement in a data flow? Are we doing something wrong or incomplete?

Thanks, Gordy

You might try explicitly managing the creation and dropping of the needed temp table in tempdb. Impact on the server is the same, just a bit more work in the code. Then you should have a table that is indistinguishable from any other.

Ken

No columns when using temp tables in T-SQL in OLEDB Source

We have a complicated select query that needs to build a couple temporary work tables that are then used in the final select statement (in an OLEDB Source data flow control). We can click preview and see the resultset, but if we click on the Columns view there are no columns. We can save and close the OLEDB Source control but downstream from it there are messages saying that there are no input columns. The T-SQL looks something like this (abbreviated):

SELECT fieldlist INTO #temp1 FROM table

SELECT fieldlist INTO #temp2 FROM table

SELECT fieldlist FROM table INNER JOIN #temp1 INNER JOIN #temp2

DROP TABLE #temp1; DROP TABLE #temp2

Has anyone been able to use temp tables in a source SQL statement in a data flow? Are we doing something wrong or incomplete?

Thanks, Gordy

You might try explicitly managing the creation and dropping of the needed temp table in tempdb. Impact on the server is the same, just a bit more work in the code. Then you should have a table that is indistinguishable from any other.

Ken

Wednesday, March 21, 2012

no bookmark lookup noticed when i think i need to see it

SELECT t2.col1 FROM Table1 t1 JOIN table2 t2 on t1.col2=t2.col2 and
t1.col3=t2.col3 returns 20 rows...
It is doing a nested loop join with t1 being the outer table and t2 being
the inner table. it is doing a non clustered index seek on t2. The
nonclustered index it is using is on (col2,col3). What I dont understand is
why on my execution plan I dont see a bookmark lookup to fetch the
corresponding row since i need t2.col1
When i run
SELECT t2.* FROM Table1 t1 JOIN table2 t2 on t1.col2=t2.col2 and
t1.col3=t2.col3
I see a bookmark lookup with the same indexes being used as the previous
query for the 2 tables...Perhaps you have a clustered index on t2(col1)? Remember that the nc indexes uses the clustering key
as row locator.
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as ugroup=microsoft.public.sqlserver
"Hassan" <fatima_ja@.hotmail.com> wrote in message news:OTXLtMWiDHA.1456@.TK2MSFTNGP11.phx.gbl...
> SELECT t2.col1 FROM Table1 t1 JOIN table2 t2 on t1.col2=t2.col2 and
> t1.col3=t2.col3 returns 20 rows...
> It is doing a nested loop join with t1 being the outer table and t2 being
> the inner table. it is doing a non clustered index seek on t2. The
> nonclustered index it is using is on (col2,col3). What I dont understand is
> why on my execution plan I dont see a bookmark lookup to fetch the
> corresponding row since i need t2.col1
> When i run
> SELECT t2.* FROM Table1 t1 JOIN table2 t2 on t1.col2=t2.col2 and
> t1.col3=t2.col3
> I see a bookmark lookup with the same indexes being used as the previous
> query for the 2 tables...
>sql

no blocking with bulk insert

I am running in QA:
Bulk insert tableA from 'C:\file.tab'
In another window ,
select * from tableA gives me no records .. I would assume some kind of
blocking while the bulk insert does it jobs. So what exactly does bulk
insert to ? The job is running and the tab file has 50 million records but
still on the second window i can query and in sp_lock all I can see for the
table is an IX tab lock and theres no blocking too. I am using SQL 2000
So whats going on ?In addition after like 15 mins, it started blocking.. So my question is what
was it doing till then.. Does it try to read the file first or something ?
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:e#4f7ykUDHA.2112@.TK2MSFTNGP10.phx.gbl...
> I am running in QA:
> Bulk insert tableA from 'C:\file.tab'
> In another window ,
> select * from tableA gives me no records .. I would assume some kind of
> blocking while the bulk insert does it jobs. So what exactly does bulk
> insert to ? The job is running and the tab file has 50 million records but
> still on the second window i can query and in sp_lock all I can see for
the
> table is an IX tab lock and theres no blocking too. I am using SQL 2000
> So whats going on ?
>
>|||No your are experiencing the effect of lock escalation. When you start the
bulk insert with no batch size or a batch size of 0 (zero) the complete file
will be inserted as one batch, so the lock accumulate and at a certain point
in time escalate to a table lock.
In order to prevent this you can take two approaches:
1) Use a batch size of 2499 to prevent the lock escalation form happening
2) Before you start the bulk insert take a rowlock on an entry in the table,
I always do this by inserting a dummy record at the end of the table. Keep
the lock. In another session do the bulk insert, because there is a lock
present you can not escalate to a table lock.
I prefer option 1.
GertD@.SQLDev.Net
Please reply only to the newsgroups.
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.
Copyright © SQLDev.Net 1991-2003 All rights reserved.
"Hassan" <fatima_ja@.hotmail.com> wrote in message
news:uBzxW5kUDHA.2012@.TK2MSFTNGP10.phx.gbl...
> In addition after like 15 mins, it started blocking.. So my question is
what
> was it doing till then.. Does it try to read the file first or something ?
>
> "Hassan" <fatima_ja@.hotmail.com> wrote in message
> news:e#4f7ykUDHA.2112@.TK2MSFTNGP10.phx.gbl...
> > I am running in QA:
> >
> > Bulk insert tableA from 'C:\file.tab'
> >
> > In another window ,
> >
> > select * from tableA gives me no records .. I would assume some kind of
> > blocking while the bulk insert does it jobs. So what exactly does bulk
> > insert to ? The job is running and the tab file has 50 million records
but
> > still on the second window i can query and in sp_lock all I can see for
> the
> > table is an IX tab lock and theres no blocking too. I am using SQL 2000
> >
> > So whats going on ?
> >
> >
> >
>

Monday, March 12, 2012

NHibernate, Views and SQL Server

Hi there,

First thing off, I'd like to know what Views are useful for? I thing it can speeds select query containing multiple join, but I'm not even sure if that's true.

Second, assuming that Views speeds thing up for SELECT query, can it be used by NHibernate when restoring object from their persistence store (in this case, SQL 2k5) ? If not, can they be (views) of any use if we use NHibernate?

Third, would it be better to create our crystal report using Views to improve performance?

Thanks for anyone who participates in this thread! :-)Views rarely speed anything up (partioning is a special case). Indexed views can help by providing a second pseudo clustered index. Generally views are to make life easy rather than faster, I'd avoid them wherever possible.

Wednesday, March 7, 2012

Newbie:Why this Select doesn't work ?

On ASP.NET 2.0, VB, I try to let user input data on TxtBox1.Text. Then I
would like to go to SQL database named PUBS and compare the "TxtBox1.Text"
against the table "job_id". If the results match, I return on the screen
"Information matched. Your password will be reset".
First, how can I establish the connection to the "pubs" and retrieve such
information ? The code below return error "job_id" not declared. Do I really
have to declare the "job_id" or something else I did is not working ?
rtial Class Default_aspx
Sub btnDefault_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim sConnect As String = ConfigurationSettings.ConnectionStrings_
("pubs").ConnectionString
Dim sSQL As String = "SELECT Employee, " _
& "WHERE job_id = TxtBox1.Text"
End Sub
End Class> Dim sSQL As String = "SELECT Employee, " _
> & "WHERE job_id = TxtBox1.Text"
Your query string should be "Select * from Employee where job_id = '" &
TxtBox1.Text & "'"
"Mr. Magoo" <maggo@.nospasm.com> wrote in message
news:ub3B3mUJFHA.1176@.TK2MSFTNGP12.phx.gbl...
> On ASP.NET 2.0, VB, I try to let user input data on TxtBox1.Text. Then I
> would like to go to SQL database named PUBS and compare the
> "TxtBox1.Text" against the table "job_id". If the results match, I return
> on the screen "Information matched. Your password will be reset".
> First, how can I establish the connection to the "pubs" and retrieve such
> information ? The code below return error "job_id" not declared. Do I
> really have to declare the "job_id" or something else I did is not working
> ?
> rtial Class Default_aspx
> Sub btnDefault_Click(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Dim sConnect As String = ConfigurationSettings.ConnectionStrings_
> ("pubs").ConnectionString
> Dim sSQL As String = "SELECT Employee, " _
> & "WHERE job_id = TxtBox1.Text"
> End Sub
> End Class
>|||There is no From clause in this select statement. You need to specify that.
Dim sSQL As String = "SELECT Employee From Pubs " _
& "WHERE job_id = " & TxtBox1.Text
Thanks
Baiju
"Mr. Magoo" <maggo@.nospasm.com> wrote in message
news:ub3B3mUJFHA.1176@.TK2MSFTNGP12.phx.gbl...
> On ASP.NET 2.0, VB, I try to let user input data on TxtBox1.Text. Then I
> would like to go to SQL database named PUBS and compare the
"TxtBox1.Text"
> against the table "job_id". If the results match, I return on the screen
> "Information matched. Your password will be reset".
> First, how can I establish the connection to the "pubs" and retrieve such
> information ? The code below return error "job_id" not declared. Do I
really
> have to declare the "job_id" or something else I did is not working ?
> rtial Class Default_aspx
> Sub btnDefault_Click(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Dim sConnect As String = ConfigurationSettings.ConnectionStrings_
> ("pubs").ConnectionString
> Dim sSQL As String = "SELECT Employee, " _
> & "WHERE job_id = TxtBox1.Text"
> End Sub
> End Class
>|||You guys are rocking !!
"Baiju" <baiju@.indus-systems.com> wrote in message
news:eBE94EVJFHA.2640@.TK2MSFTNGP09.phx.gbl...
> There is no From clause in this select statement. You need to specify
> that.
> Dim sSQL As String = "SELECT Employee From Pubs " _
> & "WHERE job_id = " & TxtBox1.Text
> Thanks
> Baiju
> "Mr. Magoo" <maggo@.nospasm.com> wrote in message
> news:ub3B3mUJFHA.1176@.TK2MSFTNGP12.phx.gbl...
> "TxtBox1.Text"
> really
>

Saturday, February 25, 2012

Newbie:UNION with SELECT TOP

I have been trying to query the Top 100 selling items for each category to b
e
one result set...all day. I thought that this should work...
SELECT TOP 100 productid, categoryid, sum(sales) as [Sales]
FROM dbo.Sales
WHERE categoryid ='01'
GROUP BY productid
ORDER BY sum(sales) DESC
UNION
SELECT TOP 100 productid, categoryid, sum(sales) as [Sales]
FROM dbo.Sales
WHERE categoryid ='02'
GROUP BY productid
ORDER BY sum(sales) DESC
UNION...
...and so on for our 14 different categories.
This is the first time that I have tried to use that UNION command, and I'm
wondering if it is the right way to go about this? Obviously I am muddling m
y
way through SQL Queries and appreciate any help.Chuck wrote:
> I have been trying to query the Top 100 selling items for each
> category to be one result set...all day. I thought that this should
> work...
> SELECT TOP 100 productid, categoryid, sum(sales) as [Sales]
> FROM dbo.Sales
> WHERE categoryid ='01'
> GROUP BY productid
> ORDER BY sum(sales) DESC
> UNION
> SELECT TOP 100 productid, categoryid, sum(sales) as [Sales]
> FROM dbo.Sales
> WHERE categoryid ='02'
> GROUP BY productid
> ORDER BY sum(sales) DESC
> UNION...
> ...and so on for our 14 different categories.
> This is the first time that I have tried to use that UNION command,
> and I'm wondering if it is the right way to go about this? Obviously
> I am muddling my way through SQL Queries and appreciate any help.
Apart from suggesting the use of UNION ALL instead of UNION*, I can make no
suggestions here. Is it not doing what you want it to do?
Bob Barrows
*Using the ALL keyword prevents the query engine from wasting time trying to
eliminate duplicate rows from the result. You know there will be no
duplicates, so use UNION ALL
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||> Apart from suggesting the use of UNION ALL instead of UNION*, I can make
> no
> suggestions here. Is it not doing what you want it to do?
Well, you can't have ORDER BY within each UNION, you can only order the
entire result.
Though I am not sure why this query all has to be in one resultset. My
three suggestions would be:
(a) get the top 100 across all categories:
SELECT TOP 100
productID, categoryID, SUM(sales)
FROM dbo.Sales
GROUP BY productID, categoryID
ORDER BY 3 DESC;
(b) an easy way to bypass the ORDER/UNION quandary is to use a #temp table
or @.table variable and insert each result there, then you can order on the
whole instead of its parts.
(c) have 14 results, or take category as a parameter and call the stored
procedure however many times is relevant (my guess is that, in general, we
are either looking at sales overall (in which case (a) is more prudent) or
we are looking at a specific category).
A|||create table #t(productid int, categoryid int, sumsales float)
insert into #t values(1,1,12.3)
insert into #t values(2,1,11.0)
insert into #t values(3,1,15.3)
insert into #t values(1,2,12.3)
insert into #t values(2,2,1.3)
insert into #t values(3,2,2.3)
insert into #t values(4,2,7.3)
insert into #t values(5,2,12.1)
select * from #t t1 where
(select count(*) from #t t2 where t1.categoryid = t2.categoryid
and t2.sumsales>t1.sumsales)<2
order by categoryid, sumsales desc
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
productid categoryid sumsales
-- --
----
3 1 15.300000000000001
1 1 12.300000000000001
1 2 12.300000000000001
5 2 12.1
(4 row(s) affected)
drop table #t|||oops I hit Post button too soon.
the query I posted will give you top 2 in each category, *assuming
there are no ties*
I added a tie:
insert into #t values(4,1,12.3)
and this works:
select * from #t t1 where sumsales in
(select top 2 with ties sumsales
from #t t2 where t1.categoryid = t2.categoryid
order by sumsales desc)
order by categoryid, sumsales desc
productid categoryid sumsales
-- --
3 1 15.300000000000001
4 1 12.300000000000001
1 1 12.300000000000001
1 2 12.300000000000001
5 2 12.1
(5 row(s) affected)|||Aaron Bertrand [SQL Server MVP] wrote:
> Well, you can't have ORDER BY within each UNION, you can only order
> the entire result.
Thanks, I missed that (obviously ... )
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||you can do it with derived tables and subqueries as well, and not have
to hard-code categoryid's
e.g.
select productid, categoryid, sales
from (
select productid, categoryid, sum(sales) as sales
from sales
group by productid, categoryid
) SumAll
where productid in (
select top 100 productid
from (
select productid, sum(sales) as sales
from sales
where categoryid=SumAll.categoryid
group by productid
) SumCategory
order by sales desc
)
order by categoryid asc, sales desc
Chuck wrote:
> I have been trying to query the Top 100 selling items for each category to
be
> one result set...all day. I thought that this should work...
> SELECT TOP 100 productid, categoryid, sum(sales) as [Sales]
> FROM dbo.Sales
> WHERE categoryid ='01'
> GROUP BY productid
> ORDER BY sum(sales) DESC
> UNION
> SELECT TOP 100 productid, categoryid, sum(sales) as [Sales]
> FROM dbo.Sales
> WHERE categoryid ='02'
> GROUP BY productid
> ORDER BY sum(sales) DESC
> UNION...
> ...and so on for our 14 different categories.
> This is the first time that I have tried to use that UNION command, and I'
m
> wondering if it is the right way to go about this? Obviously I am muddling
my
> way through SQL Queries and appreciate any help.|||
"Alexander Kuznetsov" wrote:

> oops I hit Post button too soon.
> the query I posted will give you top 2 in each category, *assuming
> there are no ties*
> I added a tie:
> insert into #t values(4,1,12.3)
> and this works:
> select * from #t t1 where sumsales in
> (select top 2 with ties sumsales
> from #t t2 where t1.categoryid = t2.categoryid
> order by sumsales desc)
> order by categoryid, sumsales desc
>
> productid categoryid sumsales
> -- --
> 3 1 15.300000000000001
> 4 1 12.300000000000001
> 1 1 12.300000000000001
> 1 2 12.300000000000001
> 5 2 12.1
> (5 row(s) affected)
>
Bob, Aaron, and Alexander,
Thank you for your quick replies. In answer to your questions...I have
thought of creating individual result sets, which would solve part of the
issue. The ultimate goal here is to create a VIEW which will join with an
inventory table to produce the percentage of Top 100 selling merchandise
"in-stock" by category. As a result, at some point it will be necessary to
rejoin the individual result sets, which I am trying to avoid.
However, I want to break down the project into bite sized pieces, focussing
for the moment on this problem.
I have tried Alexander's suggestion, but the sumsales column in it is not an
aggregate, while my query it is. That produced the error that I could not
use an aggregate as the value for an IN statement. I have altered it to
this, which has produced a timeout error.
SELECT ProductID, CategoryID, SUM(Sales) AS Expr1, SUM(QTY) AS Expr2
FROM cghastin.jvsProd_Performance t1
WHERE (ProductID IN
(SELECT TOP 2 productid
FROM cghastin.jvsprod_performance t2
WHERE t1.categoryid = t2.categoryid
GROUP BY productid
ORDER BY SUM(sales) DESC))
GROUP BY CategoryID, ProductID|||an index on (productID, category_id, sales, qty)
might speed it up
Also you may try this:
select productid, categoryid, sumsales, sumqty
from (
select productid, categoryid, sum(sales) as sumsales, sum(qty) as
sumqty
from sales
group by productid, categoryid
) t
where (select count(*) from (
select productid, categoryid, sum(sales) as sumsales, sum(qty) as
sumqty
from sales
group by productid, categoryid ) t1 where t.productid =
t1.productid
and t.categoryid = t1.categoryid and t.sumsales < t1.sumsales)<100
that is with ties|||an index on (productID, category_id, sales, qty)
might speed it up
Also you may try this:
select productid, categoryid, sumsales, sumqty
from (
select productid, categoryid, sum(sales) as sumsales, sum(qty) as
sumqty
from sales
group by productid, categoryid
) t
where (select count(*) from (
select productid, categoryid, sum(sales) as sumsales, sum(qty) as
sumqty
from sales
group by productid, categoryid ) t1 where t.productid =
t1.productid
and t.categoryid = t1.categoryid and t.sumsales < t1.sumsales)<100
that is with ties

Monday, February 20, 2012

Newbie: Select Top N - what next?

I've Just started using MS SQL 2000 with MS Access 2000. I can select
the first 10 products like this :-
SELECT top 10 dbo_products.ProductName
FROM dbo_Product
ORDER BY dbo_Product.ProductName DESC;
But when the user wants the next 10, products 11-20 and then maybe
products 21-30. How do i select them?http://www.aspfaq.com/show.asp?id=2120
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"MaD Matt" <matt@.REMOVEMEworkforcedirect.com> wrote in message
news:i9soh1hlrreb0012ri13kgf4i4a1rv8irm@.
4ax.com...
> I've Just started using MS SQL 2000 with MS Access 2000. I can select
> the first 10 products like this :-
> SELECT top 10 dbo_products.ProductName
> FROM dbo_Product
> ORDER BY dbo_Product.ProductName DESC;
> But when the user wants the next 10, products 11-20 and then maybe
> products 21-30. How do i select them?
>|||Mad
drop table #w
create table #w
(
col1 int identity,
col2 varchar(2)
)
insert into #w values ('y')--do it within a loop
select * from #w
select col1 from
(select top 10 col1
from
(select top 20 col1 from #w order by col1 ASc)AS F
order by col1 DESC) as t1
order by col1 asc
"MaD Matt" <matt@.REMOVEMEworkforcedirect.com> wrote in message
news:i9soh1hlrreb0012ri13kgf4i4a1rv8irm@.
4ax.com...
> I've Just started using MS SQL 2000 with MS Access 2000. I can select
> the first 10 products like this :-
> SELECT top 10 dbo_products.ProductName
> FROM dbo_Product
> ORDER BY dbo_Product.ProductName DESC;
> But when the user wants the next 10, products 11-20 and then maybe
> products 21-30. How do i select them?
>

Newbie: PIVOT

Hi,
SELECT * FROM SalesOrderTotalsYearly
PIVOT (SUM(SubTotal) FOR CustomerID IN ([1], [2], [4], [6])) as a
GO
The above is taken from the SQL Server 2005 T-SQL Enhancments document
used in the Microsoft Hands-On LAB.
My question is whether the IN ([1]...[6]) is always necessary?
For example if one has a large database and one wants to plot all
CustomerID's how would one go about it (I removed the IN part but got
an error!)?
Thank You
Regards,
ACB
Yes, you have to list the values which are to become columns. So you have to resort to dynamic SQL
if you don't know these values in advance. See http://aspfaq.com/show.asp?id=2462 for elaboration on
the topic.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"acb" <chribonn@.gmail.com> wrote in message
news:1137778700.663859.83660@.g44g2000cwa.googlegro ups.com...
> Hi,
> SELECT * FROM SalesOrderTotalsYearly
> PIVOT (SUM(SubTotal) FOR CustomerID IN ([1], [2], [4], [6])) as a
> GO
> The above is taken from the SQL Server 2005 T-SQL Enhancments document
> used in the Microsoft Hands-On LAB.
> My question is whether the IN ([1]...[6]) is always necessary?
> For example if one has a large database and one wants to plot all
> CustomerID's how would one go about it (I removed the IN part but got
> an error!)?
> Thank You
> Regards,
> ACB
>
|||2. Tibor Karaszi wrote:
> Yes, you have to list the values which are to become columns. So you have to resort to dynamic SQL
> if you don't know these values in advance. See http://aspfaq.com/show.asp?id=2462 for elaboration on
> the topic.
Thank You

Newbie: PIVOT

Hi,
SELECT * FROM SalesOrderTotalsYearly
PIVOT (SUM(SubTotal) FOR CustomerID IN ([1], [2], [4], [6]))
as a
GO
The above is taken from the SQL Server 2005 T-SQL Enhancments document
used in the Microsoft Hands-On LAB.
My question is whether the IN ([1]...[6]) is always necessary?
For example if one has a large database and one wants to plot all
CustomerID's how would one go about it (I removed the IN part but got
an error!)?
Thank You
Regards,
ACBYes, you have to list the values which are to become columns. So you have to
resort to dynamic SQL
if you don't know these values in advance. See /url] for elaboration on
the topic.
Tibor Karaszi, SQL Server MVP
[url]http://www.karaszi.com/sqlserver/default.asp" target="_blank">http://aspfaq.com/show.asp?id=2462[...ver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"acb" <chribonn@.gmail.com> wrote in message
news:1137778700.663859.83660@.g44g2000cwa.googlegroups.com...
> Hi,
> SELECT * FROM SalesOrderTotalsYearly
> PIVOT (SUM(SubTotal) FOR CustomerID IN ([1], [2], [4], [6]
)) as a
> GO
> The above is taken from the SQL Server 2005 T-SQL Enhancments document
> used in the Microsoft Hands-On LAB.
> My question is whether the IN ([1]...[6]) is always necessary?
> For example if one has a large database and one wants to plot all
> CustomerID's how would one go about it (I removed the IN part but got
> an error!)?
> Thank You
> Regards,
> ACB
>|||2. Tibor Karaszi wrote:
> Yes, you have to list the values which are to become columns. So you have
to resort to dynamic SQL
> if you don't know these values in advance. See 2" target="_blank">http://aspfaq.com/show.asp?id=246
2 for elaboration on
> the topic.
Thank You

Newbie: PIVOT

Hi,
SELECT * FROM SalesOrderTotalsYearly
PIVOT (SUM(SubTotal) FOR CustomerID IN ([1], [2], [4], [6])) as a
GO
The above is taken from the SQL Server 2005 T-SQL Enhancments document
used in the Microsoft Hands-On LAB.
My question is whether the IN ([1]...[6]) is always necessary?
For example if one has a large database and one wants to plot all
CustomerID's how would one go about it (I removed the IN part but got
an error!)?
Thank You
Regards,
ACBYes, you have to list the values which are to become columns. So you have to resort to dynamic SQL
if you don't know these values in advance. See http://aspfaq.com/show.asp?id=2462 for elaboration on
the topic.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"acb" <chribonn@.gmail.com> wrote in message
news:1137778700.663859.83660@.g44g2000cwa.googlegroups.com...
> Hi,
> SELECT * FROM SalesOrderTotalsYearly
> PIVOT (SUM(SubTotal) FOR CustomerID IN ([1], [2], [4], [6])) as a
> GO
> The above is taken from the SQL Server 2005 T-SQL Enhancments document
> used in the Microsoft Hands-On LAB.
> My question is whether the IN ([1]...[6]) is always necessary?
> For example if one has a large database and one wants to plot all
> CustomerID's how would one go about it (I removed the IN part but got
> an error!)?
> Thank You
> Regards,
> ACB
>|||2. Tibor Karaszi wrote:
> Yes, you have to list the values which are to become columns. So you have to resort to dynamic SQL
> if you don't know these values in advance. See http://aspfaq.com/show.asp?id=2462 for elaboration on
> the topic.
Thank You