tsql - Dynamic SQL, sp_executesql, and rebuilding the dynamic sql statement - Part 1 -


part 1: in article, "dynamic search conditions in t-sql...for sql 2005 , earlier", erland sommarskog gives example of how use dynamic sql sp_executesql.

http://www.sommarskog.se/dyn-search-2005.html#sp_executesql

select @sql =                                                  -- 19 'select o.orderid, o.orderdate, od.unitprice, od.quantity,     -- 20         c.customerid, c.companyname, c.address, c.city,        -- 21         c.region,  c.postalcode, c.country, c.phone,           -- 22         p.productid, p.productname, p.unitsinstock,            -- 23         p.unitsonorder                                         -- 24    dbo.orders o                                           -- 25  join   dbo.[order details] od on o.orderid = od.orderid       -- 26  join   dbo.customers c on o.customerid = c.customerid         -- 27  join   dbo.products p on p.productid = od.productid           -- 28       1 = 1'                                             -- 29                                                                -- 30 if @orderid not null                                        -- 31    select @sql = @sql + ' , o.orderid = @xorderid' +         -- 32                         ' , od.orderid = @xorderid'          -- 33                                                                -- 34 if @fromdate not null                                       -- 35    select @sql = @sql + ' , o.orderdate >= @xfromdate'       -- 36 

etc...

so, build dynamic sql statement, makes sense if have run 1 sp_executesql @sql variable.

however, let's suppose you've built @sql, , returned filtered records want returned, want count of records returned.

what best way go doing this?

would have declare variable, @sql_2, build identical @sql, except select statement in @sql_2 select count(*)... instead of select col1, col2, col3?

or there better approach take?

string sql statements separated semicolons. here working example returns tables in database start letter "a" , count. first simple version. returns 2 result sets, second 1 being count.

declare @findtables nvarchar(256) set @findtables = n'a%'  declare @sql nvarchar(max)  set @sql = n'set nocount on; '+ 'select * sys.tables name '''+@findtables+''';'+ 'select @@rowcount [rowcount];';  execute sp_executesql @sql 

now version variable gets valued count when need use later in stored proc.

declare @findtables nvarchar(256) set @findtables = n'a%'  declare @sql nvarchar(max) declare @parmdefinition nvarchar(500); declare @rowcount int  set @sql = n'set nocount on;  select * sys.tables name @findtablesparm; select @rowcountparm = @@rowcount; select @rowcountparm [rowcount];';  set @parmdefinition = n'@findtablesparm nvarchar(256),      @rowcountparm int output';  execute sp_executesql @sql,      @parmdefinition,      @findtablesparm=@findtables,      @rowcountparm=@rowcount output 

after runs should see 2 result sets, second 1 contain rowcount , variable @rowcount contain row count.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -