Can I use a table variable to gain output using the sp_executesql procedure?
Example:
Normally I might have something like:
DECLARE @.max varchar(9),@.sExec nvarchar(200)
SELECT @.sExec=N'SELECT @.max=(SELECT MAX(ID) FROM Parts)'
EXEC sp_executesql @.sExec, N'@.max varchar(9) OUTPUT', @.max OUTPUT
print @.max
--I'm doing something wrong... what?
--Instead can it be written to work like this?
DECLARE @.tmp TABLE(@.max varchar(9)null)
DECLARE @.sExec nvarchar(200)
SELECT @.sExec=N'INSERT INTO @.tmp =(SELECT Convert(varchar(9),ID) FROM Parts)'
N'@.tmp TABLE(@.max varchar(9)null) OUTPUT', @.tmp OUTPUT
SELECT Max(ID) FROM @.tmp
Regards,
Jamie
No but you can create a temp table before you call the sp_executesql and
access it from inside the dynamic sql. Those rows that you insert inside
the dynamic sql will still be there after the sp_executesql batch is done.
Andrew J. Kelly SQL MVP
"thejamie" <thejamie@.discussions.microsoft.com> wrote in message
news:99D6CE2F-9252-4AB9-A690-8CF00420926C@.microsoft.com...
> Can I use a table variable to gain output using the sp_executesql
> procedure?
> Example:
> Normally I might have something like:
> DECLARE @.max varchar(9),@.sExec nvarchar(200)
> SELECT @.sExec=N'SELECT @.max=(SELECT MAX(ID) FROM Parts)'
> EXEC sp_executesql @.sExec, N'@.max varchar(9) OUTPUT', @.max OUTPUT
> print @.max
> --I'm doing something wrong... what?
> --Instead can it be written to work like this?
> DECLARE @.tmp TABLE(@.max varchar(9)null)
> DECLARE @.sExec nvarchar(200)
> SELECT @.sExec=N'INSERT INTO @.tmp =(SELECT Convert(varchar(9),ID) FROM
> Parts)'
> N'@.tmp TABLE(@.max varchar(9)null) OUTPUT', @.tmp OUTPUT
> SELECT Max(ID) FROM @.tmp
> Regards,
> Jamie
|||Andrew,
Thanks for replying.
I am trying to get percentages of a single table for weighting.
If I understand right, then this should work for my first 60 percent of the
table.
DECLARE @.PercA varchar(2)
SELECT @.PercA='60'
DECLARE @.ITEMZ1 TABLE(item_no varchar(30))
SELECT @.SQL=N'(INSERT INTO @.ITEMZ1 SELECT TOP '+@.A+N' PERCENT L.item_no FROM
iminvloc_sql L INNER JOIN imitmidx_sql N ON L.loc = N.loc AND L.item_no =
N.item_no ORDER BY L.avg_cost * L.usage_ytd DESC)'
EXEC sp_executesql @.SQL, N'@.ITEMZ1 TABLE(item_no varchar(30)) OUTPUT',
@.ITEMZ1 OUTPUT
SELECT * FROM @.ITEMZ1
I'm missing something. Is it obvious?
Jamie
"Andrew J. Kelly" wrote:
> No but you can create a temp table before you call the sp_executesql and
> access it from inside the dynamic sql. Those rows that you insert inside
> the dynamic sql will still be there after the sp_executesql batch is done.
> --
> Andrew J. Kelly SQL MVP
>
> "thejamie" <thejamie@.discussions.microsoft.com> wrote in message
> news:99D6CE2F-9252-4AB9-A690-8CF00420926C@.microsoft.com...
>
>
|||I found something similar - says it works in YUKON only.
http://www.c-sharpcorner.com/Longhor...ordInYukon.asp
This is what I really want. Is there a way to do this pre-YUKON?
DECLARE @.n AS int
SET @.n = 60
SELECT TOP(@.n)
L.item_no FROM iminvloc_sql L INNER JOIN imitmidx_sql N ON L.loc = N.loc
AND L.item_no = N.item_no
ORDER BY L.avg_cost * L.usage_ytd DESC
"Andrew J. Kelly" wrote:
> No but you can create a temp table before you call the sp_executesql and
> access it from inside the dynamic sql. Those rows that you insert inside
> the dynamic sql will still be there after the sp_executesql batch is done.
> --
> Andrew J. Kelly SQL MVP
>
> "thejamie" <thejamie@.discussions.microsoft.com> wrote in message
> news:99D6CE2F-9252-4AB9-A690-8CF00420926C@.microsoft.com...
>
>
|||Suppose the tmptable needed to be created from a string variable fed into the
stored procedure:
The last line below does not work, but follows the same logic. Can a temp
table be created from an EXEC (@.SQL) statement?
DECLARE @.SQL nvarchar(4000),@.BeginPercent tinyint,@.EndPercent
tinyint,@.SQLLength int,@.FieldList varchar(4000),@.SQLTmpTbl
nvarchar(4000),@.ComputedField varchar(128)
SELECT @.SQL=N'SELECT
L.item_no,N.item_desc_1,N.item_desc_2,L.usage_ytd, L.avg_cost,L.inv_class,L.loc,L.prod_cat
FROM iminvloc_sql L INNER JOIN imitmidx_sql N ON L.loc = N.loc AND L.item_no
= N.item_no ORDER BY L.avg_cost * L.usage_ytd DESC'
SELECT @.ComputedField=N'L.usage_ytd * L.avg_cost'
DECLARE @.SQLSelect nvarchar(4000),@.SQLFROM nvarchar(4000)
SELECT @.SQLSelect=dbo.instr(@.SQL,'FROM')
SELECT @.SQLFROM=REPLACE(@.SQL,@.SQLSelect,'')
SELECT @.FieldList=LTRIM(RTRIM(REPLACE(@.SQLSelect,'SELECT' ,'')))
SELECT @.FieldList=REPLACE(@.FieldList,',',' varchar(255),')
SELECT @.FieldList=@.FieldList+' nvarchar(255)'
SELECT @.SQLTmpTbl=N'DECLARE @.tmp TABLE('+@.Fieldlist+N')'
Print REPLACE(@.SQLTmpTbl,'.','_')
exec sp_executesql @.SQLTmpTbl
"Andrew J. Kelly" wrote:
> No but you can create a temp table before you call the sp_executesql and
> access it from inside the dynamic sql. Those rows that you insert inside
> the dynamic sql will still be there after the sp_executesql batch is done.
> --
> Andrew J. Kelly SQL MVP
>
|||First of all that is a table variable and not a temp table. But in either
case it will not work as you expect. The scoping of variables and temp
tables that are created inside dynamic sql are limited to that execution
only. Once they are done the code outside of that dynamic batch will not
see those objects. Only if you create a temp table (not a table var)
outside of the dynamic sql batch will it be usable in the batch and then
again after the batch. Have a look at these:
http://www.aspfaq.com/show.asp?id=2248 Arrays & Lists
http://www.sommarskog.se/dynamic_sql.html Dynamic SQL
http://www.users.drew.edu/skass/sql/...leProc.sql.txt List to
Table script
http://www.support.microsoft.com/?id=262499 Using OutPut Params &
sp_executeSql
Andrew J. Kelly SQL MVP
"thejamie" <thejamie@.discussions.microsoft.com> wrote in message
news:BE8B9EC0-7CD2-4E71-BA09-C966895456D8@.microsoft.com...[vbcol=seagreen]
> Suppose the tmptable needed to be created from a string variable fed into
> the
> stored procedure:
> The last line below does not work, but follows the same logic. Can a
> temp
> table be created from an EXEC (@.SQL) statement?
> DECLARE @.SQL nvarchar(4000),@.BeginPercent tinyint,@.EndPercent
> tinyint,@.SQLLength int,@.FieldList varchar(4000),@.SQLTmpTbl
> nvarchar(4000),@.ComputedField varchar(128)
> SELECT @.SQL=N'SELECT
> L.item_no,N.item_desc_1,N.item_desc_2,L.usage_ytd, L.avg_cost,L.inv_class,L.loc,L.prod_cat
> FROM iminvloc_sql L INNER JOIN imitmidx_sql N ON L.loc = N.loc AND
> L.item_no
> = N.item_no ORDER BY L.avg_cost * L.usage_ytd DESC'
> SELECT @.ComputedField=N'L.usage_ytd * L.avg_cost'
> DECLARE @.SQLSelect nvarchar(4000),@.SQLFROM nvarchar(4000)
> SELECT @.SQLSelect=dbo.instr(@.SQL,'FROM')
> SELECT @.SQLFROM=REPLACE(@.SQL,@.SQLSelect,'')
> SELECT @.FieldList=LTRIM(RTRIM(REPLACE(@.SQLSelect,'SELECT' ,'')))
> SELECT @.FieldList=REPLACE(@.FieldList,',',' varchar(255),')
> SELECT @.FieldList=@.FieldList+' nvarchar(255)'
> SELECT @.SQLTmpTbl=N'DECLARE @.tmp TABLE('+@.Fieldlist+N')'
> Print REPLACE(@.SQLTmpTbl,'.','_')
> exec sp_executesql @.SQLTmpTbl
>
> "Andrew J. Kelly" wrote:
|||That worked quite well. Thank you Andrew
"Andrew J. Kelly" wrote:
> First of all that is a table variable and not a temp table. But in either
> case it will not work as you expect. The scoping of variables and temp
> tables that are created inside dynamic sql are limited to that execution
> only. Once they are done the code outside of that dynamic batch will not
> see those objects. Only if you create a temp table (not a table var)
> outside of the dynamic sql batch will it be usable in the batch and then
> again after the batch. Have a look at these:
>
> http://www.aspfaq.com/show.asp?id=2248 Arrays & Lists
> http://www.sommarskog.se/dynamic_sql.html Dynamic SQL
> http://www.users.drew.edu/skass/sql/...leProc.sql.txt List to
> Table script
> http://www.support.microsoft.com/?id=262499 Using OutPut Params &
> sp_executeSql
>
> --
> Andrew J. Kelly SQL MVP
>
> "thejamie" <thejamie@.discussions.microsoft.com> wrote in message
> news:BE8B9EC0-7CD2-4E71-BA09-C966895456D8@.microsoft.com...
>
>