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 11:32 AM
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
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?
Post #876232
Posted Wednesday, March 3, 2010 11:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:39 PM
Points: 6,825, Visits: 13,277
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
Post #876237
Posted Wednesday, March 3, 2010 11:45 AM
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
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.
Post #876246
Posted Wednesday, March 3, 2010 11:59 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:39 PM
Points: 6,825, Visits: 13,277
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
Post #876258
Posted Wednesday, March 3, 2010 1:10 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
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
...
Post #876318
Posted Wednesday, March 3, 2010 1:34 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:39 PM
Points: 6,825, Visits: 13,277
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
Post #876328
Posted Wednesday, March 3, 2010 1:54 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
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.

Post #876338
Posted Wednesday, March 3, 2010 1:58 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 3:57 PM
Points: 7,064, Visits: 15,270
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?
Post #876342
Posted Wednesday, March 3, 2010 2:05 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
Much, much closer. Still a couple things to work out, but now I understand how ROW_NUMBER works. I completely misunderstood it before.
Post #876349
Posted Wednesday, March 3, 2010 2:24 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:39 PM
Points: 6,825, Visits: 13,277
@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
Post #876362
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse