Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Querying a Table Using Dynamic SQL Expand / Collapse
Author
Message
Posted Wednesday, March 3, 2010 2:34 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, September 19, 2012 8:39 AM
Points: 595, Visits: 1,226
Yea, I noticed that, but I hard-coded the order by thinking he could adapt that if needed. It was the row_number() usage that was key.

Also, I didn't see your post before I posted, or I would not have done it, as they were similar (except for the variable order by).


Converting oxygen into carbon dioxide, since 1955.

Post #876373
Posted Wednesday, March 3, 2010 4:07 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 8:06 AM
Points: 69, Visits: 167
Well, I finally got everything to work. As I said before, I completely misunderstood the ROW_NUMBER function. Once I had that done, I was able to get the whole piece working. Not the prettiest code I have ever written, but functional.

Thanks!
Post #876424
Posted Wednesday, March 3, 2010 4:23 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 4:39 AM
Points: 6,847, Visits: 13,377
Glad it worked out for you
Once you figured how ROW_NUBER and its sibling (RANK and DENSE_RANK) work, all come in really handy.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #876434
Posted Wednesday, March 3, 2010 10:19 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
Jeff Kring (3/3/2010)
I did look into ROW_NUMBER as an option. I could use it to return the correct rows, but the application needs the actual value as well.


Not sure if this is exactly what you mean, but it's quite easy to make a new table with a new identity column whilst copying an old identity column. Read the comments in the code, please...

--===== Original table with an IDENTITY column
SELECT TOP (1000)
IDENTITY(INT,1,1) AS SomeIdentity,
NEWID() AS SomeValue
INTO #ContainsIdentity
FROM Master.sys.All_Columns

--===== New table with new IDENTITY column and old
-- IDENTITY column stripped of IDENTITY property by ISNULL
SELECT IDENTITY(INT,1,1) AS SomeNewIdentity,
ISNULL(SomeIdentity,0) AS StrippedOfIdentity
INTO #NewTable
FROM #ContainsIdentity




--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #876545
Posted Monday, July 7, 2014 12:23 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 3:33 AM
Points: 856, Visits: 1,506
Jeff, Actually OP is using (*) when in the select statement something like this

Select IDENTITY(INT,1,1) AS RowID, *
into SomeTable
From Table

When we use this as the rule of thumb, identity column will be inherit, So only one identity column per table rule violates.

I would suggested OP to use Actual Column Name List and could skipped the identity column, but as he need the 'Actual values', i think ROW_NUMBER() Function will do.
Post #1589739
Posted Monday, July 7, 2014 8:06 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 35,769, Visits: 32,437
twin.devil (7/7/2014)
Jeff, Actually OP is using (*) when in the select statement something like this

Select IDENTITY(INT,1,1) AS RowID, *
into SomeTable
From Table

When we use this as the rule of thumb, identity column will be inherit, So only one identity column per table rule violates.

I would suggested OP to use Actual Column Name List and could skipped the identity column, but as he need the 'Actual values', i think ROW_NUMBER() Function will do.

That's precisely why I didn't use SELECT *.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1589880
Posted Tuesday, July 8, 2014 12:10 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 3:33 AM
Points: 856, Visits: 1,506
yeah i really hurt when you use it in a view. Its a nightmare
Post #1590193
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse