How to solve incorrect syntax near 'xml'? - sql

I'm working on an administration program, and when writing one of it's features I encountered this error.
Here's the code.
CODE:
create procedure wIaTertiDemo
#sesiune varchar(50),
parXML xml
as
begin try
declare #utilizator varchar(500)
exec wIaUtilizator #sesiune #utilizator output
select codfiscal, denumire as #dentert, adresa
from tertiDemo
for xml raw
--create table tertiDemo(codfiscal varchar(50), denumire varchar(500), adresa varchar(500)
end try
BEGIN CATCH
DECLARE #mesajEroare varchar(1000)
SET #mesajEroare = ERROR_MESSAGE()+ '(' +OBJECT_NAME(##PROCID) + ')'
RAISERROR (#mesajEroare, 16,1)
END CATCH
Errors:
Msg 102, Level 15, State 1, Procedure wIaTertiDemo, Line 1 [Batch Start Line 0]
Incorrect syntax near 'xml'
Msg 102, Level 15, State 1, Procedure wIaTertiDemo, Line 6 [Batch Start Line 0]
Incorrect syntax near '#utilizator'
Msg 102, Level 15, State 1, Procedure wIaTertiDemo, Line 8 [Batch Start Line 0]
Incorrect syntax near '#dentert'

parXML xml
Should be:
#parXML xml
And
exec wIaUtilizator #sesiune #utilizator output
should be:
exec wIaUtilizator #sesiune, #utilizator output
And
denumire as #dentert
should be:
denumire as dentert

Related

Stored procedure for inserting the new records in the table from data table

I am getting an error while creating the below stored-procedure which would insert the new records in the table from the data table
CREATE PROCEDURE [dbo].[SP_Cde_SecureList_Upsert]
#SecureList SecureList READONLY,
#ListType varchar(20)
AS
BEGIN
IF(#ListType='FETCHDETAILS')
SELECT * FROM TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST
ELSE
BEGIN
INSERT INTO TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST([CardRefID], [KEY], [MIS_Field1], [MIS_Field2], [ValidatingCarrier]
, [OtherCarriers], [Card Vendor], [Card], [Expiration], [Precedence], [AlertEmail], [maxTktAmt], [IRP_remark])
SELECT [CardRefID], [KEY], [MIS_Field1], [MIS_Field2], [ValidatingCarrier]
, [OtherCarriers], [Card Vendor], [Card], [Expiration], [Precedence], [AlertEmail], [maxTktAmt], [IRP_remark] FROM #SecureList;
END
END
GO
Error:
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'CardRefID'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'MIS_Field1'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'MIS_Field2'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'ValidatingCarrier'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'OtherCarriers'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Card Vendor'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Card'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Expiration'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Precedence'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'AlertEmail'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'maxTktAmt'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'IRP_remark'.
Completion time
which is strange since the column names are correct
Below is the screenshot of my SP and header/columns of the table wherein I am trying to insert the data from the data table
SP and table column details screenshot
If I do select * from the data table
CREATE PROCEDURE [dbo].[SP_Cde_SecureList_Upsert]
#SecureList SecureList READONLY,
#ListType varchar(20)
AS
BEGIN
IF(#ListType='FETCHDETAILS')
SELECT * FROM [dbo].[TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST]
ELSE
BEGIN
INSERT INTO [dbo].[TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST](CardRefID, [KEY], MIS_Field1, MIS_Field2, ValidatingCarrier
, OtherCarriers, [Card Vendor], Card, Expiration, Precedence, AlertEmail, maxTktAmt, IRP_remark)
SELECT * FROM #SecureList;
END
END
GO
I am getting an error as
Msg 120, Level 15, State 1, Procedure SP_Cde_SecureList_Upsert, Line 10 [Batch Start Line 60]
The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
This doesn't have anything to do with C# so you should remove the tag from your question. Could you provide the error message?

sp_executesql failing if line ends in \

This code:
EXEC sp_executesql N'
CREATE PROCEDURE [dbo].[a]
(
#VarA NVARCHAR(200), -- Path to exe e.g. e:\blah\blah\
#VarB INT -- 1 to log actions
)
AS
BEGIN
PRINT #VarA
PRINT #VarB
END'
GO
That'll throw an error:
Msg 102, Level 15, State 1, Procedure a, Line 5 [Batch Start Line 0]
Incorrect syntax near ')'.
Msg 137, Level 15, State 2, Procedure a, Line 9 [Batch Start Line 0]
Must declare the scalar variable "#VarB".
If you remove the \ so have e:\blah\blah instead it works; if you add a space after the \ it works - it all seems to be to do with that ...
So - is that a bug?
Thanks, Dr. Michael Dye.
This is using the Backslash (Line Continuation) (Transact-SQL) feature. This means that the lines below:
(
#VarA NVARCHAR(200), -- Path to exe e.g. e:\blah\blah\
#VarB INT -- 1 to log actions
)
Are being parsed as
(
#VarA NVARCHAR(200), -- Path to exe e.g. e:\blah\blah\#VarB INT -- 1 to log actions
)
As a result, the error.
Therefore, don't end you're line with a \ and then immediately preceed it with a line break or carriage return and line break. As this is a comment, you could simply put a . at the end:
(
#VarA NVARCHAR(200), -- Path to exe e.g. e:\blah\blah\.
#VarB INT -- 1 to log actions
)

Replace function requires 3 arguments

I am trying to fetch data from SQL to excel and it is giving me the below error
My Query in SQL Server
DECLARE #Delimiter Char(1)
SET #Delimiter = CHAR(9)
EXEC MSDB.dbo.sp_Send_DBMail
#profile_name = 'K2MailSetup',
#Recipients='abs#test.com',
#Subject='Extraction Report',
#Body='Hi,
Please find attached extraction report as required. ',
#Query='set nocount on;Select Coalesce(replace(replace(A.[type], char(10), ''), char(13), ''),'') as Type FROM [EU_OTH_REG].[dbo].[TBL_EU_OTH_TXN_REG_RSDS] A',
#Attach_Query_Result_As_File = 1,
#Query_Result_Header = 1,
#Query_Attachment_Filename = 'Report.csv',
#Query_Result_Separator = #Delimiter,
#query_result_width =32767,
#query_result_no_padding=1
========================================================================
It is giving me below error
Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 517
Query execution failed: Msg 105, Level 15, State 1, Server MYKULK2DB01Q\MSSQLSTG, Line 1
Unclosed quotation mark after the character string ') as Type
FROM [EU_OTH_REG].[dbo].[TBL_EU_OTH_TXN_REG_RSDS] A
'.
Msg 102, Level 15, State 1, Server MYKULK2DB01Q\MSSQLSTG, Line 1
Incorrect syntax near ') as Type
FROM [EU_OTH_REG].[dbo].[TBL_EU_OTH_TXN_REG_RSDS] A
========================================================================
The strange thing is that when I just run the query it provides me result but when I try to create report out of it by using the above excel steps and parameters it gives me error.
You need to check the quote in the #Query variable.
I replaced it in the following;
#Query='set nocount on;Select Coalesce(replace(replace(A.[type], char(10), ''''), char(13), ''''),'''') as Type FROM [EU_OTH_REG].[dbo].[TBL_EU_OTH_TXN_REG_RSDS] A'
From the docs: SQL Server Replace
The syntax for REPLACE() goes as follows:
REPLACE ( string_expression , string_pattern , string_replacement )
As you can see, all parameters should be in string format. Make sure all parameters are encased in '' for your script to work.

Passing Cursor Result to send_dbmail as a Parameter

I've never worked with cursors before, and upon reading, this may not be the best approach, so by all means, makes suggestions.
I am attempting to pass the result set of a cursor to a query. Here's what I have so far:
DECLARE #PM varchar(50),
#c1 as CURSOR
SET #c1 = CURSOR FOR
SELECT PM
FROM PMtable
OPEN #c1;
FETCH NEXT FROM #c1 INTO #PM;
WHILE ##FETCH_STATUS = 0
BEGIN
DECLARE #emailBody nvarchar(max)
SET #emailBody = 'SELECT * FROM othertable WHERE PM = ' + #PM + ' ORDER BY PM';
EXEC msdb.dbo.sp_send_dbmail
#recipients = 'me#me.com',
#subject = 'test',
#query = #emailBody;
FETCH NEXT FROM #c1 INTO #PM;
END
CLOSE #c1;
DEALLOCATE #c1;
The idea is to send the #emailBody query result set as an email for every result in the cursor. For example, say the cursor returns three results: Bob, Jim, and Joe. I want to loop run the #emailBody query for each result from the cursor and send an email for each result.
When I run the query as is, I receive an error saying:
Msg 22050, Level 16, State 1, Line 0 Error formatting query, probably
invalid parameters
Msg 14661, Level 16, State 1, Procedure
sp_send_dbmail, Line 504 [Batch Start Line 0]
Query execution failed:
Msg 207, Level 16, State 1, Server SERVER, Line 9 Invalid column name
'Bob'.
Msg 207, Level 16, State 1, Server SERVER, Line 1 Invalid
column name 'Bob'.
I have no clue what's going on. Any ideas?
You need to add '':
SET #emailBody='SELECT * FROM othertable WHERE PM = ''' + #PM + ''' ORDER BY PM';
Be aware of possible SQL Injection.
How it works:
Msg 207, Level 16, State 1, Server SERVER, Line 9 Invalid column name 'Bob'.
SELECT * FROM othertable WHERE PM = Bob ORDER BY PM
vs.
SELECT * FROM othertable WHERE PM = 'Bob' ORDER BY PM
Please keep in mind that ORDER BY PM for one value does not change anything.

Stored procedure doesn't compile

I wrote this stored procedure and know the individual queries in there work. I just implemented a standardized template across the company which is used for exception flow.
USE [DEV_SERV]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [sch].[usp_tmSetstat]
#stat VARCHAR(50),
#statDesc VARCHAR(50),
#Parent_stat VARCHAR(50),
#Status SMALLINT
AS
BEGIN
SET NOCOUNT ON
DECLARE #Parent HIERARCHYID
DECLARE #Sibling HIERARCHYID
DECLARE #OldParent HIERARCHYID
DECLARE #NewParent HIERARCHYID
DECLARE #CurrentPos HIERARCHYID
DECLARE #StatusInactiveKey SMALLINT
BEGIN TRY
IF EXISTS (SELECT 1 FROM [sch].[stat_path] WHERE [stat_code] = #stat)
BEGIN
SET #OldParent = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat]
WHERE stat_code =
(SELECT s.parent_stat_code
FROM [sch].[stat_path] s WHERE s.stat_code = #stat))
SET #NewParent = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat]
WHERE t.stat_code = #Parent_stat);
SET #CurrentPos = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat]
WHERE t.stat_code = #stat);
UPDATE [sch].[stat] SET
stat_path = #CurrentPos.GetReparentedValue(#OldParent, #NewParent)
WHERE stat_key = #stat
GO
END
ELSE
BEGIN
SET #Parent = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat] t
WHERE t.stat_code = #Parent_stat)
SET #Sibling = (SELECT TOP 1 [stat_path]
FROM [sch].[stat] t
WHERE parent_stat_code = #Parent_stat
ORDER BY t.stat_key DESC)
SET #StatusInactiveKey = [dbo].[udf_GetStatusKey]('Active')
INSERT INTO [sch].[stat]
([stat_code],
[stat_desc],
[stat_path],
[point_type],
[status_key])
VALUES (#stat,
#statDesc,
#Parent.GetDescendant(#Sibling, NULL),
'T',
#StatusInactiveKey)
GO
END
END TRY
BEGIN CATCH
END CATCH
END
However I get these annoying errors that are like;
Msg 102, Level 15, State 1, Procedure usp_tmSetstat, Line 60
Incorrect syntax near '#stat'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'ELSE'.
Msg 137, Level 15, State 2, Line 7
Must declare the scalar variable "#Parent_stat".
Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "#Parent_stat".
Msg 137, Level 15, State 1, Line 14
Must declare the scalar variable "#StatusInactiveKey".
Msg 137, Level 15, State 2, Line 24
Must declare the scalar variable "#stat".
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'END'.
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "#stat".
Msg 137, Level 15, State 2, Line 7
Must declare the scalar variable "#errTemplate".
Msg 137, Level 15, State 2, Line 8
Must declare the scalar variable "#errTemplate".
Msg 137, Level 15, State 2, Line 9
Must declare the scalar variable "#errTemplate".
Msg 137, Level 15, State 2, Line 10
Must declare the scalar variable "#errTemplate".
Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "#errTemplate".
Msg 137, Level 15, State 1, Line 12
Must declare the scalar variable "#errProcedure".
Msg 137, Level 15, State 1, Line 13
Must declare the scalar variable "#errNumber".
Msg 137, Level 15, State 1, Line 14
Must declare the scalar variable "#errLine".
Msg 137, Level 15, State 1, Line 15
Must declare the scalar variable "#errSeverity".
Msg 137, Level 15, State 1, Line 16
Must declare the scalar variable "#errState".
Msg 137, Level 15, State 2, Line 18
Must declare the scalar variable "#errProcedure".
Msg 137, Level 15, State 2, Line 20
Must declare the scalar variable "#errTemplate".
Msg 137, Level 15, State 2, Line 25
Must declare the scalar variable "#RetVal".
I pasted my whole stored proc in there, does anyone know what's wrong? Was i supposed to declare something?
You have a GO statement in the middle of your Stored Proc. Remove it, and you should be good.