SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 dynamic query issue
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Vishal_sql
Posting Yak Master

101 Posts

Posted - 06/21/2013 :  03:55:14  Show Profile  Reply with Quote

How can i get the #TotalYears table for further use in same schema

below code doesnot allow me see the select * from #TotalYears

can anyone let me know what is issue in the script

DECLARE @TabColumns NVARCHAR(MAX)=''
DECLARE @RecCntr INT = 1
DECLARE @TotalYrs INT = 5

WHILE (@RecCntr <= @TotalYrs)
BEGIN
SET @TabColumns = @TabColumns+'Year'+ CONVERT(VARCHAR(10),@RecCntr)+' '+'REAL'+','
SET @RecCntr = @RecCntr + 1
END

DECLARE @SQLStr NVARCHAR(MAX)

IF OBJECT_ID(N'tempdb..#TotalYears', N'U') IS NOT NULL
BEGIN
DROP TABLE #TotalYears;
END

SET @SQLStr = N'CREATE TABLE #TotalYears '+' '+'(Solution VARCHAR(200),'+ @TabColumns +')'


SET @SQLStr = LEFT(@SQLStr,LEN(@SQLStr)-2) +')'
SELECT @SQLStr
EXECUTE SP_EXECUTESQL @SQLStr


select * from #TotalYears

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/21/2013 :  04:00:15  Show Profile  Reply with Quote
the problem is because temporary table is created inside dynamic sql. Why cant you create it outside in static way? where are you getting columnnames from?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000