I have table with a myNumber varchar(50) column which stores values with lead zero, like a '0000001111'.
Now I want to replace leading '0000' with '12', like '12001111'.
I tried this statement:
UPDATE myDB.dbo.myTable
SET myNumber = REPLACE(myNumber, '0000', '12')
WHERE myNumber LIKE '0000%'
But this caused an error:
Msg 248, Level 16, State 1, Procedure trplist_for_Inserted_Updated_Deleted Line 80
The conversion of the varchar value "831116399075' overflowed an int column.
Why this error caused if all columns are the varchar?
What should I do?
UPDATED
Sorry guys, the reason of error it is the tables trigger.
Here is triggers logic
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER TRIGGER [dbo].[trplist_for_Inserted_Updated_Deleted]
ON [dbo].[pList]
FOR Insert, Update, Delete
NOT FOR REPLICATION
AS
SET NOCOUNT ON
Declare #Result_check Int
Declare #NameTable nvarchar(30)
set #NameTable='plist'
Declare #FieldName nvarchar(30)
set #FieldName='ChangeTime'
Declare #Columns varbinary(MAX)
set #Columns=COLUMNS_UPDATED()
Execute CheckChangeFields #Columns, #NameTable, #FieldName, #Result_check
if #Result_check = 1
begin
return
end
set #FieldName='DateTimeInArchive'
Execute CheckChangeFields #Columns, #NameTable, #FieldName, #Result_check
if #Result_check = 1
begin
return
end
Declare #stateRecord Int
IF EXISTS(SELECT * FROM inserted)
IF EXISTS(SELECT * FROM deleted)
BEGIN
SET #stateRecord = 1 --Update
--PRINT 'Update'
END
ELSE
BEGIN
--PRINT 'Insert'
SET #stateRecord = 0 --Insert
END
ELSE
BEGIN
--PRINT 'Is DELETE'
IF EXISTS(SELECT * FROM deleted)
BEGIN
SET #stateRecord = 2 --Delete
--PRINT 'DELETE'
END
ELSE
BEGIN
SET #stateRecord = -1
--PRINT 'No DELETE'
END
END
IF #stateRecord = -1
RETURN
declare #id_value int
Declare #status_record int
declare #inn int
declare #Company int
declare #tabnumber varchar(50)
if (#stateRecord in (0,1))
BEGIN
--inserted or updated
--проверка на изменение поля StatusRecord
declare #Result_check_status_record int
if Exists(select * from Deleted where checksum(StatusRecord) In (select Checksum(StatusRecord) from Inserted))
select #Result_check_status_record= 1--одинаковые
else
select #Result_check_status_record= 0--разные
DECLARE cursor_inserted_trpList_Inserted_Updated_Delete_logs CURSOR LOCAL FOR select id, StatusRecord, INN, TabNumber, Company from inserted
OPEN cursor_inserted_trpList_Inserted_Updated_Delete_logs
FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO #id_value, #status_record,#inn, #tabnumber, #Company
WHILE ##FETCH_STATUS = 0
BEGIN
if (#inn<>'')
begin
if Exists(select id from plist where (id<> #id_value) and (INN=#inn))
begin
RollBack
RAISERROR('Данный INN уже имеется в базе', 16,2)
CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
return
end
end
if (#tabnumber<>'') and (#tabnumber<>'0')
begin
if #Company = 0
begin
if Exists(select id from plist where (id<> #id_value) and (TabNumber=#tabnumber) and (Company<=0))
begin
RollBack
RAISERROR('Данный TabNumber уже имеется в базе', 16,2)
CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
return
end
end
else
begin
if Exists(select id from plist where (id<> #id_value) and (TabNumber=#tabnumber) and (Company=#Company))
begin
RollBack
RAISERROR('Данный TabNumber уже имеется в базе в данном подразделении', 16,2)
CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
return
end
end
end
if ((#status_record&1)>0)
begin
if (#Result_check_status_record=0)
begin
Execute GustIsRelease #id_value
update guest set IDNoFace=0 where PListID=#id_value
Declare #dmtm datetime
if Exists(select id from plist where (id=#id_value) and (IsNull(DateTimeInArchive, '')=''))
begin
Update plist set DateTimeInArchive=GetDate() where (id=#id_value) and (IsNull(DateTimeInArchive, '')='')
end
end
end
else
begin
if Exists(select id from plist where (id=#id_value) and (IsNull(DateTimeInArchive, 1)<>1))
Update plist set DateTimeInArchive=Null where (id=#id_value) and (IsNull(DateTimeInArchive, 1)<>1)
end
FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO #id_value, #status_record,#inn, #tabnumber, #Company
END
CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
END
if (#stateRecord=2)
BEGIN
DECLARE cursor_inserted_trpList_Inserted_Updated_Delete_logs CURSOR LOCAL FOR select id from deleted
OPEN cursor_inserted_trpList_Inserted_Updated_Delete_logs
FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO #id_value
WHILE ##FETCH_STATUS = 0
BEGIN
if Exists(select id from pmark where owner=#id_value)
begin
RollBack
RAISERROR('У сотрудника остались активные пароли', 16,2)
CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
return
end
if Exists(select id from guest where IDNoFace=#id_value)
begin
RollBack
RAISERROR('Сотрудник привязан к посетителю', 16,2)
CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
return
end
update guest set IDNoFace=0 where IDNoFace=#id_value
update guest set ReceiveListId=0 where ReceiveListId=#id_value
FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO #id_value
END
CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
END
You could use STUFF
UPDATE myDB.dbo.myTable
SET myNumber=stuff(myNumber, 1,4, '12')
WHERE myNumber LIKE '0000%'
Use can use SUBSTRING/STUFF as below:
SUBSTRING:
UPDATE #tblTest
SET Number='12'+SUBSTRING(Number,5,LEN(NUMBER))
WHERE LEFT(Number,4)='0000'
AND LEN(Number)>4
STUFF:
UPDATE #tblTest
SET Number=STUFF(Number, 1,4, '12')
WHERE LEFT(Number,4)='0000'
AND LEN(Number)>4
Use substring function to accomplish the result u want
UPDATE myDB.dbo.myTable
SET Number='12'+SUBSTRING(myNumber,5)
WHERE SUBSTRING(myNumber,1,4)='0000';
Related
How can you improve this without using a cursor? I was thinking using table variable would help, but am I going right direction doing so? I have omitted cursor and try to do it with table variable. Please help, here is the code.
IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[BatchProcessBridge_CustomerEvent]') AND [xtype] IN (N'P'))
BEGIN
DROP PROCEDURE [dbo].[BatchProcessBridge_CustomerEvent]
END
GO
CREATE PROCEDURE [dbo].[BatchProcessBridge_CustomerEvent]
(
#BatchJobTable Bridge_CustomerEventBatchJobTable READONLY,
#Name VARCHAR(50)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Lsn BINARY(10),
DECLARE #SeqVal BINARY(10),
DECLARE #Action VARCHAR(300),
DECLARE #CustId VARCHAR(MAX)
--using tabel variable. Cursor gives bad performance.
DECLARE #TEMP_TABLE TABLE ( [Lsn] BINARY(10), [SeqVal] BINARY(10), [Action] VARCHAR(300), [CustId] VARCHAR(MAX))
INSERT INTO #TEMP_TABLE
SELECT Lsn, SeqVal, [Action], [CustId] FROM #BatchJobTable
--DECLARE GetBatchJobCursor CURSOR FAST_FORWARD
--FOR
--SELECT Lsn, SeqVal, [Action], [CustId] FROM #BatchJobTable
--OPEN GetBatchJobCursor
--FETCH NEXT FROM GetBatchJobCursor INTO #Lsn, #SeqVal, #Action, #CustId
--WHILE ##FETCH_STATUS = 0
BEGIN
BEGIN TRY
BEGIN TRANSACTION
IF (#Action = 'create')
BEGIN
-- Create.
INSERT INTO [Bridge_Customer]
(
[CustId]
,[PersonId]
,[DisplayName]
,[CreatedDate]
,[ModifiedDate]
)
SELECT
[CustId]
,[PersonId]
,[DisplayName]
,[CreatedDate]
,[ModifiedDate]
FROM
#BatchJobTable
WHERE
(Lsn = #Lsn) AND (SeqVal = #SeqVal)
END
ELSE IF (#Action = 'update')
BEGIN
-- Update.
UPDATE [Target]
SET
[Target].[CustId] = [Ref].[CustId]
,[Target].[PersonId] = [Ref].[PersonId]
,[Target].[DisplayName] = [Ref].[DisplayName]
,[Target].[CreatedDate] = [Ref].[CreatedDate]
,[Target].[ModifiedDate] = [Ref].[ModifiedDate]
FROM
[dbo].[Bridge_Customer] AS [Target]
INNER JOIN
(SELECT * FROM #BatchJobTable WHERE (Lsn = #Lsn) AND (SeqVal = #SeqVal)) AS [Ref]
ON
([Target].[CustId] = [Ref].[CustId])
END
ELSE IF (#Action = 'delete')
BEGIN
DELETE FROM [dbo].[Bridge_Customer] WHERE [CustId] = #CustId
END
-- Update last processed event.
EXEC [dbo].[UpdateLastProcessedEvent]
#Name = #Name,
#Lsn = #Lsn,
#SeqVal = #SeqVal
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE #ErrorMessage NVARCHAR(4000);
DECLARE #ErrorSeverity INT;
DECLARE #ErrorState INT;
SELECT
#ErrorMessage = ERROR_MESSAGE(),
#ErrorSeverity = ERROR_SEVERITY(),
#ErrorState = ERROR_STATE();
-- Use RAISERROR inside the CATCH block to return error
-- information about the original error that caused
-- execution to jump to the CATCH block.
RAISERROR (#ErrorMessage, -- Message text.
#ErrorSeverity, -- Severity.
#ErrorState -- State.
);
ROLLBACK TRANSACTION
END CATCH
--FETCH NEXT FROM GetBatchJobCursor INTO #Lsn, #SeqVal, #Action, #CustId
END
--CLOSE GetBatchJobCursor
--DEALLOCATE GetBatchJobCursor
END
GO
A cursor is not necessary here; this is just basic SQL. Forgive any mistakes with my code as you haven't provided any DLL but I'm pretty sure you can just do this:
IF (#Action = 'create')
INSERT INTO Bridge_Customer
(
CustId
,PersonId
,DisplayName
,CreatedDate
,ModifiedDate
)
SELECT
CustId
,PersonId
,DisplayName
,CreatedDate
,ModifiedDate
FROM #BatchJobTable
ELSE IF (#Action = 'update')
UPDATE tgt
SET tgt.CustId = Ref.CustId
,tgt.PersonId = Ref.PersonId
,tgt.DisplayName = Ref.DisplayName
,tgt.CreatedDate = Ref.CreatedDate
,tgt.ModifiedDate = Ref.ModifiedDate
FROM dbo.Bridge_Customer AS tgt
INNER JOIN #BatchJobTable AS ref
ON (tgt.CustId = Ref.CustId)
ELSE IF (#Action = 'delete')
DELETE FROM dbo.Bridge_Customer
WHERE CustId IN (SELECT CustId FROM #BatchJobTable);
Personally, I would split this into three stored and call whichever one from the application layer. What you are doing is known as a "Catch All query" which is fine but, if you must go that route, read this: Catch All Queries (Gail Shaw)
I have person table and peSubmbitedFL column. I need to write a stored procedure to update data in this column from 0 to 1 for list of certain IDs.
Where did I'm wrong with my code?
ALTER PROCEDURE [dbo].[pr_PersonAssignSubmitted]
#AppIDs varchar(8000),
#RetVal int OUTPUT
AS
SET #RetVal = 0
SET XACT_ABORT ON
BEGIN TRAN
DECLARE #AppID int
DECLARE CURSOR_COLUMNS CURSOR LOCAL FOR
SELECT item
FROM dbo.[fn_ParseStrList](#AppIDs, ',') --pretvora od string vo tabela
WHERE CAST(item as int) IN (SELECT pePersonID
FROM Person
WHERE pePersonID = #AppID)
OPEN CURSOR_COLUMNS
FETCH NEXT FROM CURSOR_COLUMNS INTO #AppID -- item stavi go vo #AppID
WHILE ##FETCH_STATUS = 0
BEGIN
UPDATE Person
SET pePersonID = #AppID, peIsSubmittedFL = 1;
FETCH NEXT FROM CURSOR_COLUMNS INTO #AppID
END
IF ##ERROR = 0 SET #RetVal = 1
COMMIT TRAN
SET XACT_ABORT OFF
WHILE ##FETCH_STATUS = 0
BEGIN
UPDATE Person
SET pePersonID = #AppID, peIsSubmittedFL = 1;
FETCH NEXT FROM CURSOR_COLUMNS INTO #AppID
END
IF ##ERROR = 0
SET #RetVal = 1
COMMIT TRAN
SET XACT_ABORT OFF
I am not sure but I think! you are need something like this.....
CREATE PROCEDURE [dbo].[pr_PersonAssignSubmitted]
#AppIDs varchar(8000)
, #RetVal int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN TRAN;
WITH PersonIDs AS
(
SELECT item AS PersonID
FROM dbo.[fn_ParseStrList](#AppIDs, ',')
)
UPDATE P
SET P.peIsSubmittedFL = 1
FROM PersonIDs i
INNER JOIN Person P ON P.pePersonID = i.PersonID
SET #RetVal = ##ROWCOUNT;
COMMIT TRAN;
SET XACT_ABORT OFF;
END
I want to write some thing like that query:
BEGIN
DECLARE #Unaloacte varchar(200);
DECLARE #Total int;
DECLARE #Flag int;
SET #Unaloacte =( Select count(PD.PropertyDetailId) from PropertyDetail AS PD join
SiteDetail AS SD ON PD.SiteDetailId=SD.SiteDetailId Where PD.CustomerId<1 and PD.SiteDetailId=27);
SET #Total= (Select count(PropertyDetailId) as Total_Count from PropertyDetail where SiteDetailId=27) ;
if( #Unaloacte = #Total)
Delete something and display message
print"Delete";
else
print"Not able to delete"
END
I hope you understand my problem.
You can try like this:
DECLARE #Msg VARCHAR(200)
if( #Unaloacte = #Total)
BEGIN
BEGIN TRAN
DELETE something
SELECT #Msg = CAST(##ROWCOUNT AS VARCHAR(10)) + ' are deleted'
RAISERROR (#Msg, 0, 1) WITH NOWAIT
COMMIT
END
ELSE
BEGIN
SELECT 'Not able to delete'
END
Also I would recommend you to use BEGIN TRAN and COMMIT if you are going to use in Production.
You can check this by USING ##ROWCOUNT and SET NOCOUNT ON
SET NOCOUNT ON
DELETE FROM TableName
IF ##ROWCOUNT > 0
PRINT 'Record Deleted'
ELSE
PRINT 'Record Not Deleted'
Here SET NOCOUNT ON is used since we dont want to see number of rows affected message.
I have this trigger that after insert update and insert data in a table, using the if as paramter for the insert or update, at the same time i have another trigger after update that changes another register. The problem is that the first trigger when does the update it trigger the update trigger... So occours that I had to protect the first trigger by checking if there´s a register there before the insert BUT I have a cursor to insert the data in the middle of all this, after I did checking the insert of one register occurs BUT the others inside the cursor aren´t inserted in the table just one register of the list in the cursor... I cannot find the problem, please help...
this is my trigger:
ALTER TRIGGER [dbo].[tr_inclusao_dupla] ON [dbo].[tb_patrimonio]
AFTER INSERT
AS
BEGIN
begin try
IF EXISTS (SELECT * FROM INSERTED) AND EXISTS (SELECT * FROM DELETED)
BEGIN
declare #id as int
declare #count as int
declare #qtd as int
declare #emlote as bit
declare #nr_serie as varchar(100)
declare #nr_patrimonio as varchar(100)
declare #dm_identificado as bit
declare #nr_inventario as varchar(100)
set #nr_patrimonio = (select nr_patrimonio_efetivo from inserted)
set #nr_serie = (select nr_serie from inserted)
set #nr_inventario = (select nr_inventario from inserted)
/*single insert*/
if (#nr_patrimonio is not null or #nr_serie is not null or #nr_inventario is not null)
begin
set #emlote =0
declare registros cursor for
select cd_patrimonio , nr_qtd_lote,nr_serie, nr_patrimonio_efetivo from inserted
open registros
fetch next from registros into #id, #qtd, #nr_serie, #nr_patrimonio
while( ##fetch_status = 0)
begin
set #dm_identificado = 1
update tb_patrimonio set dm_identificado = #dm_identificado, dm_em_lote = #emlote
where cd_patrimonio = #id
end
fetch next from registros into #id, #qtd,#nr_serie, #nr_patrimonio
close registros
deallocate registros
end
END
ELSE
BEGIN
/*multiple insert*/
begin
set #emlote =0
set #dm_identificado = 0
declare registros2 cursor for
select cd_patrimonio , nr_qtd_lote,nr_serie, nr_patrimonio_efetivo from inserted
open registros2
fetch next from registros2 into #id, #qtd, #nr_serie, #nr_patrimonio
while( ##fetch_status = 0)
begin
print #qtd
set #count = 1
update tb_patrimonio set dm_identificado = #dm_identificado, dm_em_lote = #emlote
where cd_patrimonio = #id
while (#count <= (#qtd-1))
begin
INSERT INTO [dbo].[tb_patrimonio]
([dm_patrimonio]
,[dm_em_lote]
,[nr_qtd_lote]
,[cd_grupo_produto]
,[nm_patrimonio]
,[nr_patrimonio_efetivo]
,[nr_patrimonio_antigo]
,[nr_serie]
,[ds_descricao]
,[nr_lacre]
,[cd_barra]
,[dm_tipo_entrada]
,[nr_garantia]
,[cd_nota_fiscal]
,[ds_garantia]
,[img_patrimonio]
,[cd_situacao_patrimonio]
,[cd_orgao]
,[cd_local]
,[dm_situacao]
,[cd_usuario_inc]
,[cd_estado_patrimonio]
,[cd_tipo_posse]
,[cd_usuario_alt]
,[dt_alteracao]
,[cd_usuario]
,[dt_inclusao]
,[cd_estado_equipamento]
,[cd_fornecedor]
,[nr_termo]
,[img_termo]
,[dm_identificado]
,[dt_instalacao_equipamento]
,[nr_inventario])
SELECT
[dm_patrimonio]
,#emlote
,[nr_qtd_lote]
,[cd_grupo_produto]
,[nm_patrimonio]
,[nr_patrimonio_efetivo]
,[nr_patrimonio_antigo]
,[nr_serie]
,[ds_descricao]
,[nr_lacre]
,[cd_barra]
,[dm_tipo_entrada]
,[nr_garantia]
,[cd_nota_fiscal]
,[ds_garantia]
,[img_patrimonio]
,[cd_situacao_patrimonio]
,[cd_orgao]
,[cd_local]
,[dm_situacao]
,[cd_usuario_inc]
,[cd_estado_patrimonio]
,[cd_tipo_posse]
,[cd_usuario_alt]
,[dt_alteracao]
,[cd_usuario]
,[dt_inclusao]
,[cd_estado_equipamento]
,[cd_fornecedor]
,[nr_termo]
,[img_termo]
,#dm_identificado
,[dt_instalacao_equipamento]
,[nr_inventario]
FROM inserted where cd_patrimonio = #id
set #count = #count + 1
end
fetch next from registros2 into #id, #qtd,#nr_serie, #nr_patrimonio
end
close registros2
deallocate registros2
end
END
end try
begin catch
declare #errormessage nvarchar(4000)
declare #errorseverity int
declare #errorstate int
select
#errormessage = ERROR_MESSAGE(),
#errorseverity = ERROR_SEVERITY(),
#errorstate = ERROR_STATE()
raiserror (
#errormessage,
#errorseverity,
#errorstate
)
end catch end
I am trying to develop one trigger, but I seem to be getting the following error:
A cursor with the name 'CONTACT_CURSOR' does not exist. 5339B6CC-5480-4B24-AC5D-3A4C0EAF64EC 3F0F445B-7BA3-4311-944A-E372D41FD307 The statement has been terminated.
Here is the trigger I am trying to develop:
USE [124959test]
GO
/****** Object: Trigger [dbo].[TRG_AUTOCAMPAIGN_Based_on_IDStatus_Value_Latest] Script Date: 05/28/2015 10:48:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TRG_AUTOCAMPAIGN_Based_on_IDStatus_Value_Latest]
ON [dbo].[SN_Contact2]
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF (UPDATE(contact_category)) -- selector field is the one that determines if an action shou dbe taken
BEGIN
DECLARe #CAMPAIGNSTARTTIME datetime
DECLARE #DELETEOLD INT
DECLARE #TRIGGERFIELD VARCHAR(100)
DECLARE #USERID VARCHAR(36)
DECLARE #CONTACTID VARCHAR(36)
DECLARE #CAMPAIGNID VARCHAR(36)
DECLARE #SCHEDULEFOR VARCHAR(36)
DECLARE #COMMINGLE VARCHAR(100)
DECLARE #DROPSHIP VARCHAR(100)
DECLARE #EDDM VARCHAR(100)
DECLARE #EXPEDITEDDROPSHIP VARCHAR(100)
DECLARE #EXPEDITEDSERVICES VARCHAR(100)
DECLARE #FIRSTCLASS VARCHAR (100)
DECLARE #FLATS VARCHAR(100)
DECLARE #FREIGHTROVER VARCHAR(100)
DECLARE #INTERNATIONAL VARCHAR(100)
DECLARE #MAILROVER VARCHAR(100)
DECLARE #OBBASHIP VARCHAR(100)
DECLARE #OBBATRACK VARCHAR(100)
DECLARE #PARCEL VARCHAR(100)
DECLARE #PRODUCTION VARCHAR(100)
SELECT #DELETEOLD = 0
DECLARE CONTACT_CURSOR CURSOR FOR
SELECT Contact_ContactID, SNL_RecordManagerID,contact_category,cust_commingle,CUST_Drop_Ship,
cust_eddm,CUST_Expedited_Drop_Ship,CUST_Expedited_Services,CUST_First_Class,CUST_Flats,
CUST_Freight_Rover,CUST_International,CUST_MailRover,CUST_OBBAShip,CUST_OBBATrack,
CUST_Parcel,CUST_Production FROM inserted
--where snl_recordmanagerid = '1C42B435-4A81-4109-A008-34AE91C2D48F'
OPEN CONTACT_CURSOR
FETCH NEXT FROM CONTACT_CURSOR
INTO #CONTACTID, #USERID, #TRIGGERFIELD, #COMMINGLE, #DROPSHIP, #EDDM, #EXPEDITEDDROPSHIP, #EXPEDITEDSERVICES,
#FIRSTCLASS, #FLATS, #FREIGHTROVER, #INTERNATIONAL, #MAILROVER, #OBBASHIP, #OBBATRACK, #PARCEL, #PRODUCTION
WHILE ##FETCH_STATUS = 0
BEGIN
IF #TRIGGERFIELD = 'Client' AND #USERID = '5339B6CC-5480-4B24-AC5D-3A4C0EAF64EC'
--or #USERID = '277D668E-F18F-48E7-B7C1-2FDCF50F1B36'
--or #USERID = '7FF6E867-8DDA-47FE-9A2F-E7E042FFAC56'
/* This is to launch the New Client Workflow*/
BEGIN
SET #CAMPAIGNID = 'D25053D9-EF07-4184-98F5-EC6DB1814B7E' -- CampaignID of the New client Campaign
SET #Campaignstarttime = getdate()
EXEC usp_AssignCampaign #CONTACTID, #USERID, #CAMPAIGNID, #DELETEOLD, #CAMPAIGNSTARTTIME
/* This is to launch the Send Welcome Package Workflow*/
SET #CAMPAIGNID = '08118AC6-456B-4BEC-BD09-F09A0FDC893D' -- CampaignID of the Welcome Package Campaign.
SET #Campaignstarttime = getdate()
SET #USERID = '9E283EF8-208F-4A60-BACA-555F3BECEBAB' -- The UserID of to whom the to-do task will be scheduled to.
EXEC usp_AssignCampaign #CONTACTID, #USERID, #CAMPAIGNID, #DELETEOLD, #CAMPAIGNSTARTTIME
END
ELSE
BEGIN
IF #TRIGGERFIELD = 'Prospect'
BEGIN
--Update sn_contact2 set cust_newsletter = Yes' -- When ID/Status = Prospect/Hot Prospect update Newsletter field to Yes.
SET #CAMPAIGNID = 'B1649041-FB74-40D1-807C-F969D8AE4D40' -- CampaignID of the Opt-out Email with LinkedIn Follow Up Campaign
SET #Campaignstarttime = getdate()
EXEC usp_AssignCampaign #CONTACTID, #USERID, #CAMPAIGNID, #DELETEOLD, #CAMPAIGNSTARTTIME
END
ELSE
BEGIN
IF #TRIGGERFIELD = 'Hot Prospect'
BEGIN
--Update sn_contact2 set cust_newsletter = Yes' -- When ID/Status = Prospect/Hot Prospect update Newsletter field to Yes.
SET #CAMPAIGNID = 'B1649041-FB74-40D1-807C-F969D8AE4D40' -- CampaignID of the Opt-out Email with LinkedIn Follow Up Campaign
SET #Campaignstarttime = getdate()
EXEC usp_AssignCampaign #CONTACTID, #USERID, #CAMPAIGNID, #DELETEOLD, #CAMPAIGNSTARTTIME
end
end
FETCH NEXT FROM CONTACT_CURSOR
INTO #CONTACTID, #USERID, #TRIGGERFIELD, #COMMINGLE, #DROPSHIP, #EDDM, #EXPEDITEDDROPSHIP, #EXPEDITEDSERVICES,
#FIRSTCLASS, #FLATS, #FREIGHTROVER, #INTERNATIONAL, #MAILROVER, #OBBASHIP, #OBBATRACK, #PARCEL, #PRODUCTION
END
CLOSE CONTACT_CURSOR
DEALLOCATE CONTACT_CURSOR
END
end
end
Any thoughts?
I think you have an END in the wrong place. It's pushing your Fetch Next out of scope of your Cursor declaration. Try amending the end of your code to the following...
BEGIN
--Update sn_contact2 set cust_newsletter = Yes' -- When ID/Status = Prospect/Hot Prospect update Newsletter field to Yes.
SET #CAMPAIGNID = 'B1649041-FB74-40D1-807C-F969D8AE4D40' -- CampaignID of the Opt-out Email with LinkedIn Follow Up Campaign
SET #Campaignstarttime = getdate()
EXEC usp_AssignCampaign #CONTACTID, #USERID, #CAMPAIGNID, #DELETEOLD, #CAMPAIGNSTARTTIME
end
end
END
FETCH NEXT FROM CONTACT_CURSOR
INTO #CONTACTID, #USERID, #TRIGGERFIELD, #COMMINGLE, #DROPSHIP, #EDDM, #EXPEDITEDDROPSHIP, #EXPEDITEDSERVICES,
#FIRSTCLASS, #FLATS, #FREIGHTROVER, #INTERNATIONAL, #MAILROVER, #OBBASHIP, #OBBATRACK, #PARCEL, #PRODUCTION
END
CLOSE CONTACT_CURSOR
DEALLOCATE CONTACT_CURSOR
END
end
There are couple problems with the code. First you need to move FETCH right before END that matches BEGIN after WHILE. Next thing you need to move CLOSE and DEALLOCATE to outside.
FETCH NEXT FROM CONTACT_CURSOR
INTO #CONTACTID, #USERID, #TRIGGERFIELD, #COMMINGLE, #DROPSHIP, #EDDM, #EXPEDITEDDROPSHIP,
#EXPEDITEDSERVICES, #FIRSTCLASS, #FLATS, #FREIGHTROVER, #INTERNATIONAL, #MAILROVER,
#OBBASHIP, #OBBATRACK, #PARCEL, #PRODUCTION
END
CLOSE CONTACT_CURSOR
DEALLOCATE CONTACT_CURSOR
END
END
This is basic outline of the cursor
DECLARE Some_Cursor CURSOR
OPEN Some_Cursor
FETCH NEXT FROM Some_Cursor
WHILE ##FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Some_Cursor
END
CLOSE Some_Cursor
DEALLOCATE Some_Cursor
Appreciate all the help guys! I have made this trigger running, after I verify it works correctly, I will then refine or restructure this so it would no longer be using contact cursors.
Appreciate the help!
Thanks again!
-Mike