Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Querying a Table Using Dynamic SQL


Querying a Table Using Dynamic SQL

Author
Message
Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 1226
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.


Jeff K
 Jeff K
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 170
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!
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7001 Visits: 13559
Glad it worked out for you :-D
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45047 Visits: 39898
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1376 Visits: 2597
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.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45047 Visits: 39898
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1376 Visits: 2597
yeah i really hurt when you use it in a view. Its a nightmare Hehe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search