Solved

Identity issue again

Posted on 2004-10-07
8
215 Views
Last Modified: 2010-04-17
Hi again,

Hope you can help me again
I have the following dynamic SQL statement

     EXEC('SELECT
                  u.USER
                  , IDENTITY(INT, 1, 1) AS '+''''+@Rank+''''+'
                  , ISNULL(tr.'+@Day_Name+'TOTALR, 0) AS '+''''+@TotalR+''''+'
                  ,ISNULL(tr'+@Day_Name+'TOTALP, 0) AS '+''''+@TotalP+''''+'
                  INTO '+ @TotalTable+'
                  FROM ##TOTAL '+@Day_Name+' tr
                  RIGHT JOIN #Temp_Users u ON tr.UserID = u.UserId
                  order by '+@TotalP+' DESC')

This is what it would look like if you ran it normally

           SELECT
                 u.User
                  , IDENTITY(INT, 1, 1) AS MONDAYRANK
                  , ISNULL(tr.MONDAYTOTALR, 0) AS MONDAYTOTALR
                  ,ISNULL(tr.MONDAYTOTALP, 0) AS MONDAYTOTALP
                  INTO #TEST
                  FROM ##TOTALMONDAY tr
                  RIGHT JOIN #Temp_Users u ON tr.UserID = u.UserId
                  order by MONDAYTOTALP DESC

The problem with the dynamic query is it doesn't return the correct results in the identity column.
The sorting seems all jumbled.
Whereas the original returns the correct results.

Any suggestions??



0
Comment
Question by:azrakdragon
  • 4
  • 2
  • 2
8 Comments
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 12251558
When you say it does not return the correct results in the IDENTITY column - can you be a bit more specific.
0
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12253076
try this way

     EXEC('SELECT
                  u.USER
                  , IDENTITY(INT, 1, 1) AS '+''''+@Rank+''''+'
                  , ISNULL(tr.'+@Day_Name+'TOTALR, 0) AS '+''''+@TotalR+''''+'
                  ,ISNULL(tr'+@Day_Name+'TOTALP, 0) AS '+''''+@TotalP+''''+'
                  INTO '+ @TotalTable+'
                  FROM ##TOTAL '+@Day_Name+' tr
                  RIGHT JOIN #Temp_Users u ON tr.UserID = u.UserId
                  order by ISNULL(tr'+@Day_Name+'TOTALP, 0)  DESC')

0
 

Author Comment

by:azrakdragon
ID: 12256228
Hi

Julian, when I say it gives the incorrect results, i meant it was sorting the data incorrectly

i.e.

1      0      0
2      0      0
3      0      0
4      0      0
5      0      0
6      0      0
7      0      0
8      0      0
9      0      0
10      0      0
11      0      0
12      25      50
13      0      0
14      0      0
15      0      0
16      0      0
17      243      729
18      0      0

but when I do it outside of the EXEC .. it sorts it correctly.

Vadim - thanks tried your suggestion but it still didn't work

Do you guys think it has something to do with the fact that I am using an EXEC to do this ??

Thanks



0
 

Author Comment

by:azrakdragon
ID: 12256354
Guys I think I see the problem here - it is the join although yesterday I swear I got the correct figures even with the join. Anyway - why would this be a problem?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Accepted Solution

by:
vadim_ti earned 250 total points
ID: 12256542
may be the problem because INTO clause
SELECT ....
                  INTO #TEST
try this without into

0
 

Author Comment

by:azrakdragon
ID: 12256847
Hi Vadim,
you can use the identity column without the into so I have to use it
I actually worked around the problem by changing some of my logic but would still be interested if someone knows why the join is causing this issue
It must be something to do with the identity column and the join and how it assigns an identity when you join - remember this is a right join guys

Thanks
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 12256874
Azrakdragon,

I am confused - what is wrong with the sorting in the above example - you have not given column headings and your original query has more than 3 columns.

Here are some ideas

The sort order is determined by the order by clause. I don't know what types your fields are but if they are non-numeric then the sort order will be determined by the sort order of the system. This does not explain however, why the exec produces different results from the other query.

What I would do is change your exec statement to


SELECT 'SELECT
                  u.USER
                  , IDENTITY(INT, 1, 1) AS '+''''+@Rank+''''+'
                  , ISNULL(tr.'+@Day_Name+'TOTALR, 0) AS '+''''+@TotalR+''''+'
                  ,ISNULL(tr'+@Day_Name+'TOTALP, 0) AS '+''''+@TotalP+''''+'
                  INTO '+ @TotalTable+'
                  FROM ##TOTAL '+@Day_Name+' tr
                  RIGHT JOIN #Temp_Users u ON tr.UserID = u.UserId
                  order by '+@TotalP+' DESC'

This will output the query that is actually being executed. Match that to the second query. Post the output as well - that will help
0
 

Author Comment

by:azrakdragon
ID: 12305388
Hi guys,
Well I worked around my problem, The problem was the join, so I took the join out and did it in some processing before I did the insert with the identity.
I think it had something to do with how SQL was trying to join, order by and then insert using identity all at once.
I have split points however for the help.
Thanks

Julian just for the record - this is how it was sorting when the join was in place
Rank(Identity)            TotalP
1                      0    
2                       2    
3                       0    
4                       1    
5                       3    
6                       0    

This is how it should be sorted or how it sorted after the join was taken out

Rank(Identity)            TotalP
1                      1    
2                       2    
3                       3    
4                       0    
5                       0    
6                       0    
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now