Incorrect syntax near the keyword 'WHEN' - sql-server

Trying to create a stored procedure to insert or update records depending on a field.
CREATE PROCEDURE [dbo].[usp_InsertorUpdateDB]
#dp_id char(32),
#dv_id char(32),
#em_number char(12),
#email varchar(50),
#emergency_relation char(32),
#option1 char(16),
#status char(20),
#em_id char(35),
#em_title varchar(64),
#date_hired datetime
AS
MERGE [dbo].[em] AS [Target]
USING (SELECT #dp_id, #dv_id , #em_number, #email, #emergency_relation, #option1, #status, #em_id, #em_title, #date_hired)
AS [Source] ([dp_id], [dv_id], [em_number], [email], [emergency_relation], [option1], [status], [em_id], [em_title], [date_hired])
ON [Target].[em_id] = [Source].[em_id]
WHEN MATCHED THEN
UPDATE SET [dp_id] = [Source].[dp_id],
[dv_id] = [Source].[dv_id],
[em_number] = [Source].[em_number],
[email] = [Source].[email],
[emergency_relation] = [Source].[email],
[option1] = [Source].[option1],
[status] = [Source].[status],
[em_id] = [Source].[em_id],
[em_title] = [Source].[em_title],
[date_hired] = [Source].[date_hired],
WHEN NOT MATCHED THEN
INSERT ([dp_id], [dv_id], [em_number], [email], [emergency_relation], [option1], [status], [em_id], [em_title],[date_hired])
VALUES ([Source].[dp_id], [Source].[dv_id], [Source].[em_number], [Source].[email], [Source].[emergency_relation], [Source].[option1], [Source].[status], [Source].[em_id], [Source].[em_title], [Source].[date_hired]);
GO
I am getting error on the line WHEN NOT MATCHED THEN
Incorrect syntax near the keyword 'WHEN'.
I am new to stored procedure and tried to figure it out but couldn't.

You don't want this comma -
UPDATE SET [dp_id] = [Source].[dp_id],
[dv_id] = [Source].[dv_id],
[em_number] = [Source].[em_number],
[email] = [Source].[email],
[emergency_relation] = [Source].[email],
[option1] = [Source].[option1],
[status] = [Source].[status],
[em_id] = [Source].[em_id],
[em_title] = [Source].[em_title],
[date_hired] = [Source].[date_hired]**,**
after [date_hired],

Related

Can I create folder on SQL Server Reporting Services using store procedure

i possible create new folder using gui method. Is it possible create new folder using mssql stored procedure?
You can do this, the folders are just entries in the catalog table.
The code below works but you might not need everything shown here.
You can turn this script into an SP easily, it requires three parameters to be set.
#FolderName - the name of the folder you want to create
#UserName - The name of the user you want to show as the creating user
#PolicyID - This relates (I think) to security, so I took this value from an existing folder that has the same security as the folder I wanted to create
Here's the script
DECLARE #FolderName varchar(75) = 'My Test Folder'
DECLARE #UserName varchar(75) = 'Joe.Bloggs'
DECLARE #PolicyID UNIQUEIDENTIFIER = '7e032071-6e30-46ab-9600-e0cc0a7adcf0' -- Taken from an existing folder with required permission
-- Get root folder
DECLARE #RootItemID UNIQUEIDENTIFIER = (SELECT ItemID FROM [Catalog] WHERE Type = 1 and Path = '')
-- Get creation User
DECLARE #UserID UNIQUEIDENTIFIER = (SELECT UserID FROM [Users] WHERE UserName = #UserName)
INSERT INTO [Catalog] ([ItemID], [Path], [Name], [ParentID], [Type], [Content], [Intermediate], [SnapshotDataID], [LinkSourceID], [Property], [Description], [Hidden], [CreatedByID], [CreationDate], [ModifiedByID], [ModifiedDate], [MimeType], [SnapshotLimit], [Parameter], [PolicyID], [PolicyRoot], [ExecutionFlag], [ExecutionTime], [SubType], [ComponentID])
SELECT
[ItemID] = NEWID()
, [Path] = '/' + #FolderName
, [Name] = #FolderName
, [ParentID] = #RootItemID
, [Type] = 1
, [Content] = NULL
, [Intermediate] = NULL
, [SnapshotDataID] = NULL
, [LinkSourceID] = NULL
, [Property] = '<Properties />'
, [Description] = NULL
, [Hidden] = 0
, [CreatedByID] = #UserID
, [CreationDate] = getdate()
, [ModifiedByID] = #UserID
, [ModifiedDate] = getdate()
, [MimeType] = NULL
, [SnapshotLimit] = NULL
, [Parameter] = NULL
, [PolicyID] = #PolicyID
, [PolicyRoot] = 1
, [ExecutionFlag] = 1
, [ExecutionTime] = NULL
, [SubType] = NULL
, [ComponentID] = NULL

Converting While Loop in SP to Set based

I am trying to convert a while loop stored procedure to a set-based code .Someone can enlighten me how to do this ? I separate the source data to 2 #temp table based on the #AlertType (email or sms), and inner join the #temptable to based table (itemheader and itemdetail) to replace the code below :
**Old code :**
Where IMD.CustomerAccountNo= #CustAccountNo
AND IMD.StatusCode=#ItemStatus
AND (#Reasoncode = '%' OR IMD.Reasoncode = #Reasoncode)
AND IH.TrackingNo=#TrackingNo
AND IH.ServiceTypeId = #ServiceType
**New code:**
inner join #tempresult_SMS SMS on
IMD.CustomerAccountNo= SMS.CustAccountNo
AND IMD.StatusCode= SMS.ItemStatus
AND (IMD.Reasoncode = SMS.ReasonCode 0R SMS.ReasonCode = '%')
AND IH.TrackingNo=SMS.TrackingNo
AND IH.ServiceTypeId = SMS.ServiceType
But I am struck at the part to generate the #EmailREQNumber in the while loop , how do I generate this number and insert into the table without the while loop ?
#EmailREQNumber + CAST(RANK() OVER (ORDER BY IMD.AutoID ASC) AS VARCHAR)
This is the full SP:
Alter PROCEDURE [dbo].[ezy_UpdateItemHeader](
#SMSNotificationItemDetails ezy_SMSNotificationItemDetailsType READONLY)
AS
BEGIN
SET NOCOUNT ON;
--Declare temp table
DECLARE #SMSDetailTable TABLE
(
[ROWID] INT NOT NULL PRIMARY KEY IDENTITY(1,1),
[CustAccountNo] [NVARCHAR](MAX) NULL,
[ServiceType] [NVARCHAR](MAX) NULL,
[AlertType] [NVARCHAR](MAX) NULL,
[ItemStatus] [NVARCHAR](MAX) NULL,
[TrackingNo] [VARCHAR](MAX) NULL,
[ReasonCode] [VARCHAR](MAX) NULL,
)
--Insert into temp table
INSERT INTO #SMSDetailTable( [CustAccountNo] , [ServiceType] ,[AlertType] ,[ItemStatus] ,[TrackingNo, [ReasonCode] )
SELECT [CustAccountNo] ,[ServiceType] ,[AlertType] ,[ItemStatus] ,[TrackingNo], [ReasonCode] FROM #SMSNotificationItemDetails
DECLARE #TotalRows INT , #CurrentRow INT
SELECT #TotalRows = COUNT(*) FROM #SMSDetailTable
SET #CurrentRow = 0
--Looping start in the first rows
WHILE(#CurrentRow < #TotalRows)
BEGIN
SET #CurrentRow = #CurrentRow + 1
DECLARE #CustAccountNo VARCHAR(50) ,#ServiceType VARCHAR(50) ,#ItemStatus VARCHAR(50) ,
#AlertType VARCHAR(50) , #TrackingNo VARCHAR(50) , #ReasonCode VARCHAR(50)
--Fetch the data into variable
SELECT #CustAccountNo = CustAccountNo , #ServiceType = ServiceType , #ItemStatus = ItemStatus
, #AlertType = AlertType , #TrackingNo = TrackingNo , #ReasonCode = ReasonCode
FROM #SMSDetailTable WHERE ROWID = #CurrentRow
DECLARE #REQNumber VARCHAR(100) , #EmailREQNumber VARCHAR (100)
---Build the transactionID
SET #REQNumber= 'TESTID'+replace(replace(replace(replace(CONVERT(VARCHAR(40),GETDATE(),121),'-',''),':',''),' ',''),'.','')
SET #EmailREQNumber= 'TESTEMAILID'+replace(replace(replace(replace(CONVERT(VARCHAR(40),GETDATE(),121),'-',''),':',''),' ',''),'.','')
--NOtification Item for SMS
IF (#AlertType = 'SMS' OR #AlertType = 'SMS/Email')
BEGIN
declare #NotificationItemDetail table(
[ID] [int],
[TransactionID] VARCHAR(max),
[ItemNumber] VARCHAR(max),
[AlertType] VARCHAR(max)
)
INSERT INTO #NotificationItemDetail (
[ID],
[TransactionID],
[ItemNumber],
[AlertType]
)
Select
IMD.AutoID
,#REQNumber + CAST(RANK() OVER (ORDER BY IMD.AutoID ASC) AS VARCHAR)
,#TrackingNo
,#AlertType
from ItemDetail IMD
INNER JOIN itemheader IH on
IH.TrackingNo = IMD.TrackingNo
WHERE
IMD.CustomerAccountNo= #CustAccountNo
AND IMD.StatusCode=#ItemStatus
AND (#Reasoncode = '%' OR IMD.Reasoncode = #Reasoncode)
AND IH.TrackingNo=#TrackingNo
AND IH.ServiceTypeId = #ServiceType
AND IMD.LastUpdatedDate = CAST(GETDATE() as DATE)
END
--NOtification Item for Email
IF (#AlertType = 'Email')
BEGIN
declare #NotificationItemSMSEMAILDetail table(
[ID] [int],
[ETransactionID] VARCHAR(max),
[EItemNumber] VARCHAR(max),
[EAlertType] VARCHAR(max))
INSERT INTO #NotificationItemSMSEMAILDetail (
[ID],
[ETransactionID],
[EItemNumber],
[EAlertType]
)
Select
IMD.AutoID
,#EmailREQNumber + CAST(RANK() OVER (ORDER BY IMD.AutoID ASC) AS VARCHAR)
,IMD.TrackingNo
,'Email'
from ItemDetail IMD
INNER JOIN Itemheader IH on
IH.TrackingNo = IMD.TrackingNo
where
IMD.CustomerAccountNo= #CustAccountNo
AND IMD.StatusCode=#ItemStatus
AND IH.ServiceTypeId = #ServiceType
AND IMD.LastUpdatedDate = CAST(GETDATE() as DATE)
END
END
END

How to use STRING_SPLIT() in a update query?

I want to update a table, One of its fields is a comma-separated value, I want to update that table, I tried different code but not working
UPDATE [PATS].[ReportSubscription]
SET [ScheduleID] = #ScheduleID
,[ReferenceID] = #ReferenceID
,[ReferenceType] = #ReferenceType
,[Schedule] = #Schedule
,[Day] = #Day
,[Time] = #Time
,[ProjectID] = #ProjectID
,[LastSentDate] = #LastSentDate
,[UserID] = #UserID
--,[CreatedBy] = #CreatedBy
--,[CreatedDate] = #CreatedDate
,[UpdatedBy] = #UpdatedBy
,[UpdatedDate] = #UpdatedDate
WHERE ID=#ID AND #Day IN (SELECT * FROM STRING_SPLIT(#Day,','))
I think this (SELECT * FROM STRING_SPLIT(#Day,',')) should be joined with the code.
You should be selecting value from the table returned by STRING_SPLIT:
WHERE ID=#ID AND [Day] IN (SELECT value FROM STRING_SPLIT(#Day,','))
Also, you should be comparing the table's column [Day] against the CSV list, not the variable #Day.
But, it is usually not a good idea to mix CSV data with SQL databases. Consider storing your CSV list of days in a separate table if possible.
Its possible that the data in the variable has space.
You should be selecting value from the table returned by STRING_SPLIT and triming value for use in condition, for example:
DECLARE #day NVARCHAR(500) = N'12 , 25, 41,54 ,89'
IF '41' IN (SELECT RTRIM(LTRIM(value)) FROM STRING_SPLIT(#Day,','))
PRINT('Ok')
ELSE
PRINT('Not Found')
-- printed : 'Ok'
Your query would be:
UPDATE [PATS].[ReportSubscription]
SET [ScheduleID] = #ScheduleID
,[ReferenceID] = #ReferenceID
,[ReferenceType] = #ReferenceType
,[Schedule] = #Schedule
,[Day] = #Day
,[Time] = #Time
,[ProjectID] = #ProjectID
,[LastSentDate] = #LastSentDate
,[UserID] = #UserID
,[CreatedBy] = #CreatedBy
,[CreatedDate] = #CreatedDate
,[UpdatedBy] = #UpdatedBy
,[UpdatedDate] = #UpdatedDate
WHERE ID=#ID AND Day IN (SELECT value FROM STRING_SPLIT(#Day,','))
Visit https://learn.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017 for more info.
Note: Make sure to trim your selection for unwanted spaces accordingly!

Stored procedure to update or insert

I am new to stored procedure and managed to google, to create a stored procedure for inserting or updating the database. The set of records are selected from the Oracle Database and depending on the em_id it hase to be iserted or updated in to the Sql Database table using BizTalk
Trying to create a stored procedure to insert or update records depending on a field.
CREATE PROCEDURE [dbo].[usp_InsertorUpdateDB]
#dp_id char(32),
#dv_id char(32),
#em_number char(12),
#email varchar(50),
#emergency_relation char(32),
#option1 char(16),
#status char(20),
#em_id char(35),
#em_title varchar(64),
#date_hired datetime
AS
MERGE [dbo].[em] AS [Target]
USING (SELECT #dp_id, #dv_id , #em_number, #email, #emergency_relation, #option1, #status, #em_id, #em_title, #date_hired)
AS [Source] ([dp_id], [dv_id], [em_number], [email], [emergency_relation], [option1], [status], [em_id], [em_title], [date_hired])
ON [Target].[em_id] = [Source].[em_id]
WHEN MATCHED THEN
UPDATE SET [dp_id] = [Source].[dp_id],
[dv_id] = [Source].[dv_id],
[em_number] = [Source].[em_number],
[email] = [Source].[email],
[emergency_relation] = [Source].[emergency_relation],
[option1] = [Source].[option1],
[status] = [Source].[status],
[em_id] = [Source].[em_id],
[em_title] = [Source].[em_title],
[date_hired] = [Source].[date_hired]
WHEN NOT MATCHED THEN
INSERT ([dp_id], [dv_id], [em_number], [email], [emergency_relation], [option1], [status], [em_id], [em_title],[date_hired])
VALUES ([Source].[dp_id], [Source].[dv_id], [Source].[em_number], [Source].[email], [Source].[emergency_relation], [Source].[option1], [Source].[status], [Source].[em_id], [Source].[em_title], [Source].[date_hired]);
GO
I asked a question two days before because it was showing
Incorrect syntax near the keyword 'WHEN'.
There was a comment showing the code is prone deadlock. Since I am new to Stored Procedures I dont know how to create a stored procedure for insert or update without deadlocks.I am really stuck.
Merge is the reason for deadlock, you can simply try that
CREATE PROCEDURE [dbo].[usp_InsertorUpdateDB]
#dp_id char(32),
#dv_id char(32),
#em_number char(12),
#email varchar(50),
#emergency_relation char(32),
#option1 char(16),
#status char(20),
#em_id char(35),
#em_title varchar(64),
#date_hired datetime
AS
IF ( (SELECT COUNT(em_id) FROM [dbo].[em] WHERE em_id = #em_id) > 0)
UPDATE [dbo].[em]
SET
[dp_id] = #dp_id,
[dv_id] = #dv_id,
[em_number] = #em_number,
[email] = #email,
[emergency_relation] = #emergency_relation,
[option1] = #option1,
[status] = #status,
[em_id] = #em_id,
[em_title] = #em_title,
[date_hired] = #date_hired
WHERE em_id = #em_id
ELSE
INSERT INTO [dbo].[em] (dp_id, dv_id, em_number, email, emergency_relation, option1, [status], em_id, em_title,date_hired)
VALUES (#dp_id, #dv_id, #em_number, #email, #emergency_relation, #option1, #status, #em_id, #em_title, #date_hired);
GO

Values from 2 variables are being swapped in stored procedure

I have modified a stored procedure and added an extra parameter to be added to a table. The problem is the value for the added parameter is being swapped with an original parameter.
ALTER PROCEDURE [dbo].[spWeb_BulkGenerateWRsDevices]
(
#WorkRequestDeviceId nvarchar(max) = NULL,
#TechnicianId int = NULL,
#RequiredBy datetime = NULL,
#Priority int = NULL,
#WRTypeId int,
#WorkRequestSummary nvarchar(255) = NULL,
#AuthorOfRequest int,
#Contact nvarchar(40) = NULL,
#PhoneNo nvarchar(20) = NULL,
#StatusId int,
#Created datetime = NULL,
#CustomerId int
)
AS
/* Update the WR record */
BEGIN
DECLARE #COUNT INT
SET #COUNT =(SELECT COUNT(*) FROM WorkRequest WHERE WorkRequestDeviceId IN(SELECT stringval FROM dbo.CSV(#WorkRequestDeviceId)))
IF #COUNT >0
BEGIN
DECLARE #InsertedRows AS TABLE (WorkRequestId int,WorkRequestDeviceId int)
INSERT INTO WorkRequest
(AllocatedTo,RequiredBy,Priority,WRTypeId,WorkRequestSummary,AuthorOfRequest,
Contact,PhoneNo,StatusId,DateOfRequest,WorkRequestDeviceId,CustomerId)
OUTPUT Inserted.WorkRequestId,Inserted.WorkRequestDeviceId INTO #InsertedRows
SELECT #TechnicianId ,#RequiredBy,#Priority,#WRTypeId,#WorkRequestSummary,#AuthorOfRequest,#Contact
,#PhoneNo,#StatusId,#Created,#CustomerId,* from dbo.fnSplit(#WorkRequestDeviceId, ',')
END
IF #COUNT = 0
BEGIN
INSERT INTO WorkRequest
(WorkRequestDeviceId)
SELECT * from dbo.fnSplit(#WorkRequestDeviceId, ',')
UPDATE WorkRequest
SET
AllocatedTo =#TechnicianId ,
RequiredBy = #RequiredBy,
Priority = #Priority,
WRTypeId = #WRTypeId,
WorkRequestSummary =#WorkRequestSummary,
AuthorOfRequest= #AuthorOfRequest,
Contact = #Contact,
PhoneNo = #PhoneNo,
StatusId = #StatusId,
DateOfRequest = #Created,
CustomerId = #CustomerId
WHERE WorkRequestDeviceId IN(SELECT stringval FROM dbo.CSV(#WorkRequestDeviceId))
END
I have added the CustomerId parameter. When I call the procedure and say set WorkRequestDeviceId = 312 and CustomerId = 148 the table WorkRequest has a new record but with WorkRequestDeviceId = 148 and CustomerId = 312. I am new to stored procedure so sorry for my ignorance, any ideas?
James
Your insert is transposing the variables in the insert statement and the select.
Change your insert to read:
INSERT INTO WorkRequest
(AllocatedTo,RequiredBy,Priority,WRTypeId,WorkRequestSummary,AuthorOfRequest,
Contact,PhoneNo,StatusId,DateOfRequest,CustomerId,WorkRequestDeviceId)
And all should be fine.

Resources