May 31, 2016 at 9:58 am
Hi,
I have a table. Its containing query to create a view.
Create Table #Temp (Query Varchar(8000)
Insert into #Temp Select 'Create View Vw_Name as'
Insert into #Temp Select 'Select Col1,Col2,....Col15 from Table_20160501'
Insert into #Temp Select 'Select Col1,Col2,....Col15 from Table_20160502'
.
.
.
Insert into #Temp Select 'Select Col1,Col2,....Col15 from Table_20160531'
Total length of the query in a temp table is 25K.
Now i want to create the view which is available in a #temp table.
Thanks in Advance...
May 31, 2016 at 10:13 am
based on your design, i'd guess you have to refresh this view every day, since it looks like you build one table per day.
i would bet that those individual tables might actually come from yet another table that already has all the data for all days/months/years in it, is it possible to find and query that data, or aggregate it into another table?
Create View Vw_Name
as
Select Col1,Col2,....Col15 from Table_20160501 UNION ALL
Select Col1,Col2,....Col15 from Table_20160502 UNION ALL
--...
Select Col1,Col2,....Col15 from Table_20160531
Lowell
May 31, 2016 at 10:13 am
balu.arunkumar (5/31/2016)
Hi,I have a table. Its containing query to create a view.
Create Table #Temp (Query Varchar(8000)
Insert into #Temp Select 'Create View Vw_Name as'
Insert into #Temp Select 'Select Col1,Col2,....Col15 from Table_20160501'
Insert into #Temp Select 'Select Col1,Col2,....Col15 from Table_20160502'
.
.
.
Insert into #Temp Select 'Select Col1,Col2,....Col15 from Table_20160531'
Total length of the query in a temp table is 25K.
Now i want to create the view which is available in a #temp table.
Thanks in Advance...
You can't use a temp table in a view. It doesn't make sense. What would happen if the temp table doesn't exist?? Why not use a few UNIONS and forget the temp table entirely?
Something like this.
Create View Vw_Name as
Select Col1, Col2, Col15 from Table_20160501 UNION ALL
Select Col1, Col2, Col15 from Table_20160502 UNION ALL
Select Col1, Col2, Col15 from Table_20160531
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 31, 2016 at 10:15 am
Something like this?
DECLARE @sql varchar(max);
SET @sql = (SELECT Query + CHAR(10) FROM #Temp FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)');
EXEC( @sql);
Or this:
DECLARE @sql varchar(max) = '';
SELECT @sql += Query + CHAR(10)
FROM #Temp;
EXEC( @sql);
Remember to include UNION ALL if you don't have them in your table.
June 1, 2016 at 7:59 am
Thanks lot Luis Cazares... Its working..
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply