Author |
Topic |
tribune
Posting Yak Master
105 Posts |
Posted - 2005-01-26 : 14:24:26
|
I'm attempting to get a scalar integer value, specificly the count(*) of a dyanmic query, and produce it as an output parameter of a stored procedure. Since the procedure itself produces only a subset the actual records (say records 1-99), I don't know the total count of the records, e.g. 1-99 of 1,000.Suppose my query is "select count(*) from customers"set @SQL = "select count(*) from customers"set @TotalResultsCount = cast(exec(@SQL) as int)@TotalResultsCount is specific as an "int output" in the sproc parameter.How do I do this? Thanks! |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-01-26 : 14:52:08
|
[code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myCounts99(mySPID int, myCount int)GODECLARE @sql varchar(8000)SELECT @sql = 'DELETE FROM myCounts99 WHERE mySPID = ' + CONVERT(varchar(4),@@SPID) + CHAR(13) + 'INSERT INTO myCounts99(mySPID, myCount) SELECT ' + CONVERT(varchar(4),@@SPID) + ', COUNT(*) FROM Orders'SELECT @sqlEXEC(@sql)DECLARE @x intSELECT @x = myCount FROM myCounts99 WHERE mySPID = @@SPIDSELECT @xGOSET NOCOUNT OFFDROP TABLE myCounts99GO[/code]Brett8-) |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-01-26 : 16:19:07
|
ordeclare @SQL nvarchar(1000)delare @i intset @SQL = 'select @i = count(*) from customers'exec sp_executesql @sql, N'@i int out', @i outset @TotalResultsCount = @iseehttp://www.mindsdoor.net/SQLTsql/sp_executeSQL.html==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-01-26 : 17:06:07
|
OUTThat's what I was missing...DECLARE @Count intexecute sp_executesql N'select @Count = COUNT(*) from Northwind.dbo.Orders', N'@Count int OUT', @Count OUTSELECT @Count Brett8-) |
|
|
tribune
Posting Yak Master
105 Posts |
Posted - 2005-02-03 : 19:23:35
|
Thank you very much! |
|
|
David D
Starting Member
1 Post |
Posted - 2005-04-06 : 05:23:09
|
quote: Originally posted by nr ordeclare @SQL nvarchar(1000)delare @i intset @SQL = 'select @i = count(*) from customers'exec sp_executesql @sql, N'@i int out', @i outset @TotalResultsCount = @i
What would be the solution if table 'customers' is also a variable?f.e. @tableI always get an error: "Must declare the variable '@table'." |
|
|
AndyB13
Aged Yak Warrior
583 Posts |
Posted - 2005-04-06 : 05:54:16
|
This isnt recommended at all, it can be doneWhat do you think would happen if you got this to work?egEXECUTE YourSproc 'YourBigTable DROP YourBigTable'Start a new thread outlining exactly what you want to do and any code/sample data you have done so farEdit:Welcome to the forum Beauty is in the eyes of the beerholder |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-04-06 : 07:56:49
|
declare @SQL nvarchar(1000)delare @i intset @SQL = 'select @i = count(*) from ' + @tableexec sp_executesql @sql, N'@i int out', @i outset @TotalResultsCount = @i==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
|