Stored procedure doesn't compile - sql

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.

Related

I am trying to copy from one column to another in same table but getting msg 50000 and error 3609 as well

UPDATE poitem_mst
SET Uf_TYRI_poitem_req_ship_date=promise_date
Should be simple, but I get these errors:
Msg 50000, Level 16, State 1, Procedure RaiseErrorSp, Line 121 [Batch Start Line 0]
Purchase Order that has [PO: P000010685] does not exist.
Msg 3609, Level 16, State 1, Line 1
The transaction ended in the trigger. The batch has been aborted.
The PO number changes every time I run that query.

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?

How to solve incorrect syntax near 'xml'?

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

How do I create an eventlog event upon a SQL connectivity issue? Here's my concrete example:

I need to connect to an external database and the connectivity may sometimes fail. However, I am seeing that if the SELECT statement below fails, the IF statement after that does not execute at all:
SELECT COUNT(*) FROM [DB].[View]
IF ##ERROR <> 0
BEGIN
EXEC xp_logevent 63000, 'Issue!', error
END
Here's the error message:
Msg 8180, Level 16, State 1, Line 32
Statement(s) could not be prepared.
Msg 4413, Level 16, State 1, Line 32
Could not use view or function 'DB.view' because of binding errors.
Msg 208, Level 16, State 1, Procedure Servers2Switchs, Line 60
Invalid object name xxx.

Why does this update query not work?

I am pretty new with SQL Server and DBMS and I have some problem with the following query that upade a single record of a table named: MaliciousCodeAlertDocument
This is my query:
UPDATE MaliciousCodeAlertDocument
SET Discovered = GetDate(),
LastUpdated = GetDate(),
SourceId = "UP",
MalCodeID = 1,
Title = "UPDATED",
Origin = "UP",
Type = "UP",
Feature = "UP",
Risk = "UP",
Severity = 1,
OverallImpact = 1,
ContagionPotential = 1,
Wild = "UP",
AlertStatusID = 1,
AttributeType = 1,
DetailLevel = 1 ,
Language = 1 ,
Summary = "UP" ,
Symptom = "UP" ,
TechnicalDescription = "UP" ,
MitigatingStrategy = "UP" ,
Disinfection = "UP" ,
URL = "UP"
WHERE Id = 11316
When I try to execute it I obtain the following error message:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UPDATED'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
What is the problem? How can I fix it?
Tnx
Using double-quotes makes SQL Server think that the value within the quotes is a column, but it looks like you're trying to update it to a text value. If you use single quotes instead, SQL will recognize that you want to insert that value into your field, and should load it correctly.
EDIT: Sorry, didn't see that #scragar already posted this solution. Anyone know if I'm supposed to delete my answer because of this?