SQL Clone
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
Jeff K
 Jeff K
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 170
I have run into an interesting dilemma. I am working on a stored procedure used by one of our applications. Table Name is being passed in as a variable and I need to query that table. As part of the result set, I need to return rownumber. I handled this by using SELECT INTO and IDENTITY (INT, 1, 1).
DECLARE @V_QUERY_STR VARCHAR(MAX),
@V_TABLE_NAME VARCHAR(50),
@V_START_ROW INT,
@V_QUERY_CNT INT

SELECT @V_TABLE_NAME = 'AHL_Deed', @V_START_ROW = 1

SELECT @V_QUERY_STR = 'SELECT IDENTITY(INT, 1,1) as ROWINT, TN.* INTO #temp4 '
+ 'FROM ' + @V_TABLE_NAME + ' TN '
+ @V_ORDER_BY_QUERY
+ 'DECLARE @V_QUERY_CNT INT;'
+ 'SELECT @V_QUERY_CNT = COUNT(*) FROM ' + @V_TABLE_NAME + ';'

+ 'SELECT * FROM #temp4 WHERE ROWINT BETWEEN ' + CAST(@V_START_ROW AS VARCHAR) + ' AND ' + CAST(COALESCE(@V_QUERY_CNT, @V_START_ROW) AS VARCHAR)

EXECUTE (@V_QUERY_STR)



Here's the catch...the AHL_Deed table already has an identity column so I get the following error:
Msg 8108, Level 16, State 1, Line 1
Cannot add identity column, using the SELECT INTO statement, to table '#temp4', which already has column 'Deed_ID' that inherits the identity property.

My only other thought would be to incorporate a cursor to handle the insert, but I'm not sure how I could incorporate the row number column. Any suggestions?
LutzM
LutzM
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23987 Visits: 13559
Did you look into ROW_NUMBER function (assuming you're using SQL2005)?



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 K
 Jeff K
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 170
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.
LutzM
LutzM
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

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


I don't understand what you refer to as "actual value". Please elaborate.
The best way would be to provide ddl and sample data for two sample table including expected result so we have something to play with.



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 K
 Jeff K
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 170
CREATE TABLE deed (deed_id INT IDENTITY(1,1), deed_name VARCHAR(25))
GO

INSERT INTO deed (deed_name) VALUES ('Deed F')
INSERT INTO deed (deed_name) VALUES ('Deed C')
INSERT INTO deed (deed_name) VALUES ('Deed I')
INSERT INTO deed (deed_name) VALUES ('Deed G')
INSERT INTO deed (deed_name) VALUES ('Deed B')
INSERT INTO deed (deed_name) VALUES ('Deed L')
INSERT INTO deed (deed_name) VALUES ('Deed D')
INSERT INTO deed (deed_name) VALUES ('Deed A')
INSERT INTO deed (deed_name) VALUES ('Deed J')
INSERT INTO deed (deed_name) VALUES ('Deed H')
INSERT INTO deed (deed_name) VALUES ('Deed K')
INSERT INTO deed (deed_name) VALUES ('Deed E')
GO

DECLARE @V_QUERY_STR VARCHAR(MAX),
@V_TABLE_NAME VARCHAR(50),
@V_START_ROW INT,
@V_QUERY_CNT INT,
@V_ORDER_BY_QUERY VARCHAR(50)

SELECT @V_TABLE_NAME = 'deed', @V_START_ROW = 1, @V_ORDER_BY_QUERY = 'ORDER BY deed_name'

SELECT @V_QUERY_STR = 'SELECT IDENTITY(INT, 1,1) as ROWINT, TN.* INTO #temp4 '
+ ' FROM ' + @V_TABLE_NAME + ' TN '
+ @V_ORDER_BY_QUERY
+ ' DECLARE @V_QUERY_CNT INT;'
+ ' SELECT @V_QUERY_CNT = COUNT(*) FROM ' + @V_TABLE_NAME + ';'

+ ' SELECT * FROM #temp4 WHERE ROWINT BETWEEN ' + CAST(@V_START_ROW AS VARCHAR) + ' AND ' + CAST(COALESCE(@V_QUERY_CNT, @V_START_ROW) AS VARCHAR)

EXECUTE (@V_QUERY_STR)


The result set I need would be:
1 Deed A
2 Deed B
3 Deed C
4 Deed D
...
LutzM
LutzM
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23987 Visits: 13559
Would something like the following will do it?
Side note: this will only work if you always provide an ORDER BY clause since this is required for the ROW_NUMBER function....

SELECT @V_QUERY_STR = 'SELECT ROW_NUMBER() OVER(' + @V_ORDER_BY_QUERY +')
as ROWINT, TN.* INTO #temp4 '
+ ' FROM ' + @V_TABLE_NAME + ' TN '
+ @V_ORDER_BY_QUERY
+ ' DECLARE @V_QUERY_CNT INT;'
+ ' SELECT @V_QUERY_CNT = COUNT(*) FROM ' + @V_TABLE_NAME + ';'

+ ' SELECT * FROM #temp4 WHERE ROWINT BETWEEN ' + CAST(@V_START_ROW AS VARCHAR) + ' AND ' + CAST(COALESCE(@V_QUERY_CNT, @V_START_ROW) AS VARCHAR)

EXECUTE (@V_QUERY_STR)





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
Steve Cullen
Steve Cullen
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: 1363 Visits: 1226
USE dbTools
GO

CREATE TABLE deed (deed_id INT IDENTITY(1,1), deed_name VARCHAR(25))
GO

INSERT INTO deed (deed_name) VALUES ('Deed F')
INSERT INTO deed (deed_name) VALUES ('Deed C')
INSERT INTO deed (deed_name) VALUES ('Deed I')
INSERT INTO deed (deed_name) VALUES ('Deed G')
INSERT INTO deed (deed_name) VALUES ('Deed B')
INSERT INTO deed (deed_name) VALUES ('Deed L')
INSERT INTO deed (deed_name) VALUES ('Deed D')
INSERT INTO deed (deed_name) VALUES ('Deed A')
INSERT INTO deed (deed_name) VALUES ('Deed J')
INSERT INTO deed (deed_name) VALUES ('Deed H')
INSERT INTO deed (deed_name) VALUES ('Deed K')
INSERT INTO deed (deed_name) VALUES ('Deed E')
GO

DECLARE @V_QUERY_STR VARCHAR(MAX),
@V_TABLE_NAME VARCHAR(50),
@V_START_ROW INT,
@V_QUERY_CNT INT,
@V_ORDER_BY_QUERY VARCHAR(50)

SELECT @V_TABLE_NAME = 'deed', @V_START_ROW = 1, @V_ORDER_BY_QUERY = 'ORDER BY deed_name'

SELECT @V_QUERY_STR = 'SELECT ROW_NUMBER() OVER (ORDER BY deed_name) as ROWINT, TN.* INTO #temp4 '
+ ' FROM ' + @V_TABLE_NAME + ' TN '
+ @V_ORDER_BY_QUERY
+ ' DECLARE @V_QUERY_CNT INT;'
+ ' SELECT @V_QUERY_CNT = COUNT(*) FROM ' + @V_TABLE_NAME + ';'

+ ' SELECT * FROM #temp4'

EXECUTE (@V_QUERY_STR)





Converting oxygen into carbon dioxide, since 1955.


Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29817 Visits: 19009
keep in mind that lutz' version will keep the identity on the deed_id column. Given the usage you're describing it shouldn't make a diffierence, but this means the DEED_ID would increment if another row were to be inserted, and not the new rownumber.

If that doesn't fit your usage, you'd have to explicitly pass the columns from the table, using

cast(deed_id as int) deed_int

to "disable" the identity from the existing column.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Jeff K
 Jeff K
SSC-Enthusiastic
SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)SSC-Enthusiastic (131 reputation)

Group: General Forum Members
Points: 131 Visits: 170
Much, much closer. Still a couple things to work out, but now I understand how ROW_NUMBER works. I completely misunderstood it before.
LutzM
LutzM
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23987 Visits: 13559
@Steve:
The disadvantage of your solution is that you have to have a aclumn named "deed_name" within each table you want to apply that dynamic query to.
Therefore, I decided to use a variable for the OVER clause, too.
It's also easier to control the order of the output values. Example: You could easily run it with
@V_ORDER_BY_QUERY = 'ORDER BY deed_name DESC '
without any changes to the dynamic part.



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
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