Did not find exact example - sql-server

I am getting this error:
Msg 102, Level 15, State 1, Line 9 Incorrect syntax near '+'.
When executing the following code:
DECLARE #Source nVARCHAR(30)
set #source = 'Srce.srce'
select #Source
--drop table #temp1
select 'xx' col1
INTO #temp
from #Source + .dbo.table1

You can not pass the name of database through variable.
If you want to forward the name of the database using the variable you have to use dynamic SQL.

Related

Copy column from one table to another table with condition

ALTER PROCEDURE [dbo].[Timesheet_update]
AS
BEGIN
DECLARE #sql2 nvarchar(max), #status nvarchar(1)
SET #sql2 = 'insert into s21022020 (s21_stfno) select m_stfno from master where m_status<>'D''
EXECUTE (#sql2)
END
EXECUTE Timesheet_update
Results in an error:
Msg 207, Level 16, State 1, Line 23
Invalid column name 'D'.
m_status column contain data =D
I don't understand why you feel the need to make this a dynamic SQL - just write the statement directly inside the stored procedure - like this:
ALTER PROCEDURE [dbo].[Timesheet_update]
AS
BEGIN
INSERT INTO s21022020 (s21_stfno)
SELECT m_stfno
FROM master
WHERE m_status <> 'D'
END

Reversing COLUMN DATA by dynamically passing table name and Column Name

I want to reverse a COLUMN DATA dynamically by passing table name and Column Name
DECLARE #TABLE_NAME NVARCHAR(300) -- to pass table name dynamically
DECLARE #COLUMN_NAME NVARCHAR(300) -- to pass column name dynamically
SET #TABLE_NAME = 'TEST1' -- Passing table name
SET #COLUMN_NAME = 'SSN' -- Passing column Name
DECLARE #OUTPUT NVARCHAR(MAX) -- to pass the selected column data to update
SET #OUTPUT = 'SELECT'+' '+#COLUMN_NAME+' '+'FROM'+#TABLE_NAME
DECLARE #UPDATE_EXEC NVARCHAR(MAX) -- To exec the Final update result
SET #UPDATE_EXEC = 'UPDATE ['+#TABLE_Name + ']'+'SET'+' '+#COLUMN_NAME+
'=REVERSE(EXEC (#output))'
EXEC(#UPDATE_EXEC)
But I'm getting these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'EXEC'.
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "#output"
You are trying to put EXEC in your update query, its not allowed. EXEC is use to execute the query, it can't be part of any query.
To reverse any column in your table you can do so like below,
SET #UPDATE_EXEC = 'UPDATE ['+#TABLE_Name + ']'+'SET'+' '+#COLUMN_NAME+ '=REVERSE('+#COLUMN_NAME+')';
EXEC(#UPDATE_EXEC);

Compilation when Column doesn't exist

I've a Stored procedure which looks like this:
create procedure test as
begin
if exists(
select 1 from sys.columns
where Name = N'Column2'
and Object_ID = Object_ID(Table2')
)
select Column2 from Table2
end
I want to run this procedure on db where Column2 doesn't exist. I don't want to take existence check out of SP. Currently the error is :
Msg 207, Level 16, State 1, Procedure test, Line
39 [Batch Start Line 0] Invalid column name 'Column2'.
Is there any way to do so? Why yes and why not?
and why for instance if you check for existence table and select non-existent table that works?
Use dynamic SQL:
create procedure test as
begin
if exists (select 1
from sys.columns
where Name = N'Column2' and Object_ID = Object_ID('Table2')
)
begin
exec sp_executesql N'select Column2 from Table2';
end;
end;

EXEC select query not working with temp table SQL Server 2005

I have this SQL Server 2005 Puzzle
The following code doesn't work
DECLARE #tmp TABLE (ID int IDENTITY PRIMARY KEY , strDateTime varchar(50))
INSERT INTO #tmp VALUES('1/2/13')
DECLARE #x varchar(1000)
SET #x = 'SELECT * FROM ' + #tmp ;
EXEC (#x)
I get the following error
Msg 137, Level 15, State 2, Line 5
Must declare the scalar variable "#tmp".
If I do this
SET #x = 'SELECT * FROM #tmp ' ;
I get this error
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "#tmp".
Well what I was really trying to do is passing dynamic column name to a select statement and I found that I didn't like that either
Can someone guide me what I'm doing wrong
Thanks
A table variable is only valid in the scope within which it is created:
A table variable behaves like a local variable. It has a well-defined
scope. This is the function, stored procedure, or batch that it is
declared in.
To do this, you'll need to use a temporary table:
CREATE TABLE #tmp (ID int IDENTITY PRIMARY KEY , strDateTime varchar(50))
INSERT INTO #tmp VALUES('1/2/13')
DECLARE #x varchar(1000)
SET #x = 'SELECT * FROM #tmp';
EXEC (#x)

XQuery/XPath uses sql parameter?

I am try to implement the following code.
declare #para varchar(10) = 'b';
declare #x xml = '
<x>
<a>1111</a>
<b>2222</b>
<c>3333</c>
</x>';
select #x.query('/x/sql:variable("#para")');
The above code should get the node of <b>2222</b>. However, it get the following error
Msg 9335, Level 16, State 1, Line 8
XQuery [query()]: The XQuery syntax '/function()' is not supported.
declare #para varchar(10) = 'b';
declare #x xml = '
<x>
<a>1111</a>
<b>2222</b>
<c>3333</c>
</x>';
select #x.query('/x/*[local-name()=sql:variable("#para")]');

Resources