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")]');
Related
The aim here is to read a specific value from a different server and store the return value in a local parameter for use later.
Here is the error code:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'Alarm'.
Here is the code I have tried:
declare #sql_string nvarchar(400);
declare #inhostnamn nvarchar(100) = 'BLUE65\SQLEXPRESS'
declare #inuser nvarchar(50) = 'dev1'
declare #password1 nvarchar(50) = 'dev1'
declare #database nvarchar(100) = 'Test_destroy'
declare #count_posts varchar(10)
declare #tabellnamn varchar(50) = 'Alarms'
declare #last_read_alarm varchar(30)
set #tabellnamn = 'Logg'
set #sql_string = N'set #last_read_alarm1 = cast(last_read as nvarchar(30)) select * from openrowset (''SQLNCLI'', ''Server='+#inhostnamn+';UID='+#inuser+';Pwd='+#password1+';Database='+#database+';Persist Security Info=True'',''select Last_ID FROM '+#database +'.dbo.Logg where Tables_sql=''Alarm'' '')';
print 'string =' + #sql_string;
exec sp_executesql #sql_string, N'#last_read_alarm1 varchar(30) OUTPUT', #last_read_alarm1=#last_read_alarm OUTPUT;
select #last_read_alarm
print #last_read_alarm;
Now I am stuck. I cannot see the error I have made, and am hoping for a couple of different eyes.
Thanks to Andrei Odegov for great assistance. It helped putting 4 ' on each side.
The correct code would be in my case now:
set #sql_string = N'select #last_read_alarm= (select * from openrowset (''SQLNCLI'', ''Server='+#inhostnamn+';UID='+#inuser+';Pwd='+#password1+';Database='+#database+';Persist Security Info=True'',''select Last_ID FROM '+#database +'.dbo.Logg where Tables_sql='''''+#tmp_str+''''' ''))';
So the answer from the query will now end up in local variable #last_read_alarm.
My first question on Stack Overflow :)
I have XML:
DECLARE #xml XML = '<root><tag1 /><tag2 /></root>';
I need to remove node, but, path to node is variable "#path".
DECLARE #path XML = '/root/tag2';
My query is:
SET #xml.[modify]('delete sql:variable("#path")');
But, I get error:
Msg 9342, Level 16, State 1, Line 9
XQuery [modify()]: An XML instance is only supported as the direct source of an insert using sql:column/sql:variable.
So my question is: how can I delete xml node by sql parameter?
There is no general recipie...
Just some ideas:
If you know the node's name
DECLARE #xml XML = '<root><tag1 /><tag2 /></root>';
DECLARE #nodeToDelete VARCHAR(100)='tag2';
SET #xml.modify('delete (/root/*[local-name()=sql:variable("#nodeToDelete")])[1]');
SELECT #xml;
If you know the node's name with FLWOR-query
DECLARE #xml XML = '<root><tag1 /><tag2 /></root>';
DECLARE #nodeToDelete VARCHAR(100)='tag2';
SET #xml=#xml.query('<root>
{
for $nd in /root/*[local-name()!=sql:variable("#nodeToDelete")]
return $nd
}
</root>');
SELECT #xml;
dynamically created
DECLARE #xpath VARCHAR(100)='/root/tag2';
DECLARE #command VARCHAR(MAX)=
'DECLARE #xml XML = ''<root><tag1 /><tag2 /></root>'';
SELECT #xml;
SET #xml.modify(''delete ' + #xpath + ''');
SELECT #xml';
PRINT #command;
EXEC(#command);
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.
I have problems with my dynamical ms sql query. Can someone help me. Here is my code. Problem is inside the OPENQUERY, near '1033'
DECLARE #sql nvarchar(max);
DECLARE #server nvarchar(255) = (SELECT [Value] FROM [WarehouseMgmt].[SyncConfig] WHERE [Key] = 'ReportServerLinkedServer')
DECLARE #database nvarchar(255) = (SELECT [Value] FROM [WarehouseMgmt].[SyncConfig] WHERE [Key] = 'ReportServerDatabase')
SET #sql = 'MERGE [WarehouseMgmt].[DimReportServerReports] AS DRSR
USING (SELECT ItemId,Name FROM OPENQUERY('+#server+',''SELECT ItemId,Name FROM '+#database+'.[dbo].[Catalog] WHERE Type=2 AND Name NOT LIKE ''1033%'' AND Path NOT LIKE ''/Reports/%Subs'')
) AS CATALOG
ON (DRSR.[SourceOrigId] = [Catalog].[ItemId])
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
[SourceOrigId],
[ReportName],
SyncExecId
)
VALUES
(
[Catalog].[ItemId],
ISNULL([Catalog].[Name],''<UNKNOWN>''),
#SyncExecId
)
OUTPUT
[Catalog].[ItemId],
[Catalog].[Name]
INTO #NewReportServerReports;'
EXEC sp_executesql #sql,N'#SyncExecId int',#SyncExecId
Error code is:
Msg 50000, Level 11, State 1, Procedure WriteJobLog, Line 101 Error
writing job log: Line #90: [ERR] #2: Incorrect syntax near '1033'.
Added few quotes. Try this.
SET #sql = 'MERGE [WarehouseMgmt].[DimReportServerReports] AS DRSR
USING (SELECT ItemId,Name FROM OPENQUERY('+#server+',''SELECT ItemId,Name FROM '+#database+'.[dbo].[Catalog] WHERE
Type=2 AND Name NOT LIKE ''''1033%'''' AND Path NOT LIKE ''''/Reports/%Subs'''''')
) AS CATALOG
ON (DRSR.[SourceOrigId] = [Catalog].[ItemId])
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
[SourceOrigId],
[ReportName],
SyncExecId
)
VALUES
(
[Catalog].[ItemId],
ISNULL([Catalog].[Name],''<UNKNOWN>''),
#SyncExecId
)
OUTPUT
[Catalog].[ItemId],
[Catalog].[Name]
INTO #NewReportServerReports;'
--print #sql
EXEC sp_executesql #sql,N'#SyncExecId int',#SyncExecId
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)