SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Issue with Sp_executeSql table variable output


Issue with Sp_executeSql table variable output

Author
Message
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5450 Visits: 4076
My requirement is i need to populate @tblAmount dynamically.

Here is the code
DECLARE @AdjustmentBatch_ID INT

DECLARE @Platform VARCHAR(20), @Type INT, @lStr NVARCHAR(1000),@TableName VARCHAR(20),@ColName VARCHAR(20)
,@parameters NVARCHAR(30)
SELECT @Platform = '',@lStr = '',@TableName = '',@ColName = '', @AdjustmentBatch_ID = 15



--DECLARE @tblAmount TABLE(LegalEntity VARCHAR(10), Amount MONEY )

--BEGIN TRY

SELECT @Type = type,@Platform = Platform FROM dbo.Adjustment_Batch ab(NOLOCK)
WHERE ab.AdjustmentBatch_ID = @AdjustmentBatch_ID AND ab.Status = 1

SELECT @TableName = CASE
WHEN @Type = 1 THEN 'GLEntry'
WHEN @Type = 2 THEN 'GrossPremium'
END,
@ColName = CASE
WHEN @Type = 1 THEN 'BaseAmount'
WHEN @Type = 2 THEN 'GrossWrittenPremium'
END

SELECT @lStr = N'DECLARE @tblAmount TABLE(LegalEntity VARCHAR(10), Amount MONEY );INSERT INTO @tblAmount (LegalEntity, Amount)'+
'SELECT LegalEntity,SUM(CAST('+ @ColName +' as MONEY)) AS Amount FROM dbo.' + @TableName + ' t(NOLOCK)'+
'WHERE t.AdjustmentBatch_ID = ' + CAST(@AdjustmentBatch_ID AS VARCHAR(20)) + ' GROUP BY t.LegalEntity '

PRINT @lStr
EXEC SP_executeSql @lstr
,@parameters = N'@tblAmount(LegalEntity VARCHAR(10), Amount MONEY ) TABLE OUTPUT'
,@tblAmount



Error :
Msg 137, Level 15, State 2, Line 45
Must declare the scalar variable "@tblAmount".


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
s_osborne2
s_osborne2
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1017 Visits: 2292
Hi,

The last line of your query references @tblAmount but you haven't declared it and given it a type. That's why it's failing.

Thanks,


Simon



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5450 Visits: 4076
Now i have modified this.
Please see . its still failing

      DECLARE @AdjustmentBatch_ID INT

DECLARE @Platform VARCHAR(20), @Type INT, @lStr NVARCHAR(1000),@TableName VARCHAR(20),@ColName VARCHAR(20)
,@parameters NVARCHAR(30)
SELECT @Platform = '',@lStr = '',@TableName = '',@ColName = '', @AdjustmentBatch_ID = 15



DECLARE @tblAmountOut TABLE(LegalEntity VARCHAR(10), Amount MONEY )

--BEGIN TRY

SELECT @Type = type,@Platform = Platform FROM dbo.Adjustment_Batch ab(NOLOCK)
WHERE ab.AdjustmentBatch_ID = @AdjustmentBatch_ID AND ab.Status = 1

SELECT @TableName = CASE
WHEN @Type = 1 THEN 'GLEntry'
WHEN @Type = 2 THEN 'GrossPremium'
WHEN @Type = 3 THEN 'GrossClaim'
WHEN @Type = 4 THEN 'CededPremium'
WHEN @Type = 5 THEN 'CededClaim'
END,
@ColName = CASE
WHEN @Type = 1 THEN 'BaseAmount'
WHEN @Type = 2 THEN 'GrossWrittenPremium'
WHEN @Type = 3 THEN 'IndemnityPaidLoss'
WHEN @Type = 4 THEN 'CededWrittenPremium'
WHEN @Type = 5 THEN 'ReinsuaranceIndemnityPaidLoss'
END

SELECT @lStr = N'DECLARE @tblAmount TABLE(LegalEntity VARCHAR(10), Amount MONEY );INSERT INTO @tblAmount (LegalEntity, Amount)'+
'SELECT LegalEntity,SUM(CAST('+ @ColName +' as MONEY)) AS Amount FROM dbo.' + @TableName + ' t(NOLOCK)'+
'WHERE t.AdjustmentBatch_ID = ' + CAST(@AdjustmentBatch_ID AS VARCHAR(20)) + ' GROUP BY t.LegalEntity '

PRINT @lStr
EXEC SP_executeSql @lstr
,@parameters = N'@tblAmountOut(LegalEntity VARCHAR(10), Amount MONEY ) TABLE OUTPUT '
,@tblAmountOut = @tblAmount OUTPUT




-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Kurt W. Zimmerman
Kurt W. Zimmerman
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: 1426 Visits: 1398
Are you getting the same error? Which table is it complaining about? @tblAmount?

Kurt

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
Kurt W. Zimmerman
Kurt W. Zimmerman
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: 1426 Visits: 1398
I don't use SP_executeSQL often... but I'm guessing that if you declare the @tblAmount table in the @parameters variable instead of the SQL string (@lStr) it may work.

Kurt

Kurt W. Zimmerman
SR DBA
Lefrak Organization
New York, NY

http://www.linkedin.com/in/kurtwzimmerman
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5450 Visits: 4076
can any body help me to populate table variable using dynamic query a i posted above. i had hard time to resolve this .

the above dynamic sql get tablename based on type id THEN we need to fetch some data from select query and populate table variable @tblAmount

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
saravanan.a 88885
saravanan.a 88885
Mr or Mrs. 500
Mr or Mrs. 500 (534 reputation)Mr or Mrs. 500 (534 reputation)Mr or Mrs. 500 (534 reputation)Mr or Mrs. 500 (534 reputation)Mr or Mrs. 500 (534 reputation)Mr or Mrs. 500 (534 reputation)Mr or Mrs. 500 (534 reputation)Mr or Mrs. 500 (534 reputation)

Group: General Forum Members
Points: 534 Visits: 788
try to replace the following part in your coding i hope it will work for you .

SELECT @lStr = N'DECLARE @tblAmount TABLE(LegalEntity VARCHAR(10), Amount MONEY );INSERT INTO @tblAmount (LegalEntity, Amount)'+
'SELECT LegalEntity,SUM(CAST('+ @ColName +' as MONEY)) AS Amount FROM dbo.' + @TableName + ' t(NOLOCK)'+
'WHERE t.AdjustmentBatch_ID = ' + CAST(@AdjustmentBatch_ID AS VARCHAR(20)) + '
GROUP BY t.LegalEntity select * from @tblAmount'

PRINT @lStr
EXEC SP_executeSql @lstr
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