This works:
drop table #A
select v = getdate()
into #A
select * from #A
But not this (no result):
drop table #A
create table #A (v varchar(30))
declare #x varchar(30) = 'select v = getdate() into #A'
execute(#x)
select * from #A
I need to be able to do this above one to address a scenario. Must be simple and silly, but just trying to understand why this just doesn't work like first one?
Please advise. Thanks experts!
I can propose workaround and move all you script to #x variable.
So your script will be as:
declare #x nvarchar(MAX) = '
drop table IF EXISTS #A
select v=getdate() into #A
select * from #A'
PRINT(#x)
EXEC(#x)
MSDN says
SELECT…INTO creates a new table in the default filegroup and inserts the resulting rows from the query into it.
So if you create table prior to select into, you cannot use select into, you need to use insert into
Related
im tring this piece of code..
declareing a variable and than adding GETDATE() with casting so that table name is unique.. and than inserting all the data from the existing table to the new backuptbl i jux made..
begin
declare #st varchar(200);
SET #st = 'tblNameBackup_'+CAST(getdate() as varchar(100));
PRINT #st
create table mytbl -- <<-- i wish to set my table name as #st
(
tID int,
tName nchar(20)
)
select * into mytbl from thet-able-whose-backup-to-be-taken;
end
actually i wish to backup my table at every transaction therefore i was trying this.. i know this is not the real situation (as i am also doing this for practice if we can do this or not) hope u understand my question thanx for any help
You'll need to make dynamic SQL, if I understand you correct, and execute that:
Something similar to this:
declare #st varchar(200);
SET #st = 'tblNameBackup_'+CAST(getdate() as varchar(100));
EXEC('create table ' + #st + '
(
tID int,
tName nchar(20)
)
select * into mytbl from thet-able-whose-backup-to-be-taken;');
Be careful about injection possibilities, also instead of just EXEC you can wrap it in sp_executesql or similar. It's just to illustrate the method.
I just cant get this to work within a procedure (as a simple query this works fine):
SET #c:=0;
SELECT
(#c := #c + 1) AS Cumulative,
FROM
NumberTable,
Table
...
I think it hast something to to with the "counter"-variable. For the procedure I tried:
DECLARE c int(4) DEFAULT 0;
BEGIN
SELECT
(c = c + 1) AS Cumulative,
FROM
NumberTable,
Table
...
END
But that doesn't work (gives NULL). Any suggestions?
PS: I think the question breaks down to this: How to SET a variable within SELECT while using a PROCEDURE (since := does not work there)
I am copying the data to temporary table using select statement which produces the result of pivoting.
My procedure is:
alter procedure proc11 AS
create Table #Temp (Software varchar(max), Ver varchar(max))
declare #cols varchar(max)
declare #colTabl varchar(max)
declare #alter varchar(max)
set #cols=STUFF((select distinct ',[' + Ltrim(rtrim(YEAR(Dt))) +']' from temp FOR XML PATH('')),1,1,'');
set #colTabl=STUFF((select distinct '[' + Ltrim(rtrim(YEAR(Dt))) +'] int,' from temp FOR XML PATH('')),1,0,'');
set #colTabl= LEFT(#colTabl,len(#colTabl)-1);
set #alter = 'alter table #Temp add '+ #colTabl;
exec(#alter)
EXEC('select * into #Temp from
(select YEAR(Dt)[year],Software,Ver from temp)T
Pivot(count([year]) for [year] in ('+#cols+'))PVT')
But the statement select * from #Temp returns no record.
How do I copy these records.
Please help. Thanks in advance.
It's because '#' temporary tables are kept just for the duration of current connection, and visible only in it's scope. Even if you create a temp table, open another tab in SSMS and try to use it, you you'll see "table does not exist" error.
What happens is, that your EXEC statement create the #Temp table, and when it finieshes, the table is immediately dropped. Not only that - #Temp table from CREATE statement, and #Temp from EXEC statement are in fact different tables! Try creating two #temp tables in different SSMS tabs and you'll see they're in fact not the same.
You could use global temp tables. Just change the name to ##Temp and it should work.
Run these two examples.
This won't work:
EXEC('select * into #Temp from (select ''something'' as x) x');
select * from #Temp
And this one will (when you run it second time you'll notice "table already exists" error:
EXEC('select * into ##Temp from (select ''something'' as x) x');
select * from ##Temp
EXEC('insert #Temp select * from
(select YEAR(Dt)[year],Software,Ver from temp)T
Pivot(count([year]) for [year] in ('+#cols+'))PVT order by Software')
Or simply remove the INSERT from the dynamic portion:
INSERT INTO #temp
EXEC(select * from
(select YEAR(Dt)[year],Software,Ver from temp)T
Pivot(count([year]) for [year] in ('+#cols+'))PVT order by Software')
I wish to compare a variable #a containing an entire column of a table with another variable containing a single value. Is it possible?
create procedure pc1(#var int)
as begin
declare #a int
select #a=id from tb1
while(if exists(#var=#a))
begin
select * from tb1 where id=#var
end
return
end
There is no direct way but you can do this thing in following way.
declare #a int
Set #a = 1
if exists(
select #a as ID
Intersect
Select ID
from tbl
)
Print('Hello')
MY Table like this:
id Tag platform
1 #class1,#class2 CS
2 #class1 PS
3 #class2 CS
if i pass "'#class1'" as parameter to SP getting only one record that is 2nd record.But need to 1st and 2nd records because #class1 contains in both 1,2 rows.Please tell me how to write this.I am using IN statement as of now.By using getting only record.
MY SP:
ALTER PROCEDURE [dbo].[usp_Get]-- 1,"'#class1,#class2'"
#Appid INT,
#TagList NVARCHAR (MAX)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT #TagList = '%' + RTRIM(LTRIM(#TagList)) + '%';
declare #tags varchar(MAX)
set #tags = #TagList
create table #t (tag varchar(MAX))
set #tags = 'insert #t select ' + replace(#tags, ',', ' union select ')
exec(#tags)
Select
id FROM dbo.List WHERE ((appid=#Appid)) AND ((Tags LIKE(select tag from #t)
END
How to modify please tell me...
Thanks in advance..
One solution would be to use LIKE operator in your stored procedure:
CREATE PROCEDURE FindTag #TagName char(50)
AS
SELECT #TagName = '%' + TRIM(#TagName) + '%';
SELECT Tag
FROM MyTable
WHERE Tag LIKE #TagName;
GO