Insert Stored Procedure with null parameters - sql

I want to create an insert stored procedure with null parameters, if pass the value for that parameter then it has to insert or update in database
My stored procedure is:
Create proc [dbo].[SP_InsertOrUpdateCourseDetails]
#CourseID int,
#Tab1Title nvarchar(250) = null,
#Tab1Description nvarchar(max) = null,
#Tab2Title nvarchar(250) = null,
#Tab2Description nvarchar(max) = null,
#Tab3Title nvarchar(250) = null,
#Tab3Description nvarchar(max) = null,
#Tab4Title nvarchar(250) = null,
#Tab4Description nvarchar(max) = null,
#Syllabus nvarchar(max) = null
As
Begin
If NOT EXISTS (Select * from CourseDetail Where CourseID=#CourseID )
Begin
Insert into CourseDetail(CourseID, Tab1Title, Tab1Description,
Tab2Title, Tab2Description,
Tab3Title, Tab3Description, Tab4Title, Tab4Description,
Syllabus)
values (#CourseID, #Tab1Title, #Tab1Description, #Tab2Title, #Tab2Description,
#Tab3Title, #Tab3Description, #Tab4Title, #Tab4Description, #Syllabus)
IF ##ERROR = 0 AND ##ROWCOUNT =1
Begin
Select top 1 CourseID from CourseDetail Order by CourseDetailID Desc
End
Else
Begin
Select 0
End
End
Else
Begin
Update CourseDetail
SET
Tab1Title = #Tab1Title,
Tab1Description = #Tab1Description,
Tab2Title = #Tab2Title,
Tab2Description = #Tab2Description,
Tab3Title = #Tab3Title,
Tab3Description = #Tab3Description,
Tab4Title = #Tab4Title,
Tab4Description = #Tab4Description,
Syllabus = #Syllabus
Where
CourseID = #CourseID
IF ##ERROR = 0 AND ##ROWCOUNT =1
Begin
Select top 1 CourseID from CourseDetail Order by CourseDetailID Desc
End
Else
Begin
Select 0
End
End
End

You can use ISNULL function passing in it SP's parameter as first parameter and default value in case of insert or actual column's value in case of update:
Insert into CourseDetail(CourseID, Tab1Title, Tab1Description,
Tab2Title, Tab2Description,
Tab3Title, Tab3Description, Tab4Title, Tab4Description,
Syllabus)
values (#CourseID, ISNULL(#Tab1Title, 'default value'), ISNULL(#Tab1Description,'default value'), ...
Update CourseDetail
SET
Tab1Title = ISNULL(#Tab1Title, Tab1Title),
Tab1Description = ISNULL(#Tab1Description, Tab1Description),
....
Where
CourseID = #CourseID
But in this case you won't can to set NULL value explicitely even if you'll need

Related

BizTalk 2013R2 SQL Query Timeouts

Using BizTalk to call a SQL Server stored procedure (see below), which:
Writes data to 5 tables
Checks each table to see if the record exists
Updates or Inserts accordingly
The problem is that when BizTalk receives hundreds of calls, the stored procedure is called each time which appears to add an overhead to the SQL Server and eventually causes timeout errors returned in BizTalk, resulting in the data not being written to the database.
Can anyone advise on the best way to optimise my query so that it processes these tables without much overhead, or have I got it optimised enough already?
USE [MDH]
GO
/****** Object: StoredProcedure [dbo].[spcuPersonStudentProgrammeModule] Script Date: 8/15/2022 2:29:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spcuPersonStudentProgrammeModule]
-- person/personref params
#student_id VARCHAR(max)
,#account_id VARCHAR(max) = NULL
,#prefix_honorific VARCHAR(max) = NULL
,#first_name VARCHAR(max) = NULL
,#middle_name VARCHAR(max) = NULL
,#surname VARCHAR(max) = NULL
,#familiar_name VARCHAR(max) = NULL
,#date_of_birth DATE = NULL
,#external_email_address VARCHAR(max) = NULL
,#mobile_phone_no VARCHAR(max) = NULL
,#gender VARCHAR(max) = NULL
,#ethnicity VARCHAR(max) = NULL
,#domicile VARCHAR(max) = NULL
,#disability VARCHAR(max) = NULL
,#nationality VARCHAR(max) = NULL
,#telephone_no VARCHAR(max) = NULL
,#prev_surname VARCHAR(max) = NULL
,#country_of_birth VARCHAR(max) = NULL
-- student params
,#student_email_address VARCHAR(max) = NULL
,#currently_studying_flag VARCHAR(max) = NULL
,#HesaStudentID VARCHAR(max) = NULL
,#UCAS_ID VARCHAR(max) = NULL
,#uln VARCHAR(max) = NULL
,#VisaReq VARCHAR(max) = NULL
,#PurposeOfResidency VARCHAR(max) = NULL
,#cas_status VARCHAR(max) = NULL
,#student_status VARCHAR(max) = NULL
,#source_system VARCHAR(max) = NULL
,#main_programme_code VARCHAR(max) = NULL
,#type VARCHAR(max) = NULL
,#student_support_no VARCHAR(max) = NULL
,#exam_id VARCHAR(max) = NULL
,#su_opt VARCHAR(max) = NULL
,#change_type VARCHAR(max) = NULL
,#international_sponsored_students varchar(80) = null
,#visa_type VARCHAR(max) = null
-- student_programmes params
,#programme_code VARCHAR(50)
,#programme_description VARCHAR(MAX) = NULL
,#start_date DATETIME = NULL
,#end_date DATETIME = NULL
,#mdh_stage_code VARCHAR(MAX) = NULL
,#main_award_flag VARCHAR(10) = NULL
,#load_category VARCHAR(10) = NULL
,#qualification_level VARCHAR(10) = NULL
,#student_study_level VARCHAR(10) = NULL
,#school_code VARCHAR(10) = NULL
,#college_code VARCHAR(10) = NULL
,#campus_code VARCHAR(10) = NULL
,#graduate_yn VARCHAR(10) = NULL
,#is_wbdl VARCHAR(80) = NULL
,#ul_qual_aim VARCHAR(MAX) = NULL
,#ul_qual_aim_desc VARCHAR(MAX) = NULL
-- student_modules params
,#module_code VARCHAR(50)
,#module_desc VARCHAR(MAX) = NULL
,#mod_date_time DATETIME = NULL
-- student_address params
,#perm_address1 VARCHAR(50) = NULL
,#perm_address2 VARCHAR(50) = NULL
,#perm_address3 VARCHAR(50) = NULL
,#perm_address4 VARCHAR(50) = NULL
,#perm_address5 VARCHAR(50) = NULL
,#perm_postcode VARCHAR(50) = NULL
,#perm_country_code VARCHAR(50) = NULL
,#term_address1 VARCHAR(50) = NULL
,#term_address2 VARCHAR(50) = NULL
,#term_address3 VARCHAR(50) = NULL
,#term_address4 VARCHAR(50) = NULL
,#term_address5 VARCHAR(50) = NULL
,#term_postcode VARCHAR(50) = NULL
,#term_country_code VARCHAR(50) = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE #person_id UNIQUEIDENTIFIER
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
-- Create/Update person_test/person_reference_test
IF EXISTS ( SELECT person_id FROM dbo.person_reference WHERE account_id = #account_id and ISNULL(status,'') <> 'Delete')
BEGIN
SELECT 'Student exists, updating'
SET #person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = #account_id and ISNULL(status,'') <> 'Delete')
UPDATE person
SET prefix_honorific = CASE WHEN #prefix_honorific = 'null' or #prefix_honorific is null or #prefix_honorific = '' THEN prefix_honorific ELSE #prefix_honorific END
,first_name = ISNULL(#first_name, first_name)
,middle_name = ISNULL(#middle_name, middle_name)
,surname = ISNULL(#surname, surname)
,familiar_name = #familiar_name
,date_of_birth = ISNULL(#date_of_birth, date_of_birth)
,external_email_address = ISNULL(#external_email_address, external_email_address)
,gender = ISNULL(#gender, gender)
,ethnicity = ISNULL(#ethnicity, ethnicity)
,domicile = ISNULL(#domicile, domicile)
,disability = ISNULL(#telephone_no, disability)
,telephone_no = ISNULL(#telephone_no, telephone_no)
,prev_surname = ISNULL(#prev_surname, prev_surname)
,country_of_birth = ISNULL(#country_of_birth, country_of_birth)
,proc_date_time = GETDATE()
,mobile_phone_no = ISNULL(#mobile_phone_no, mobile_phone_no)
,nationality = ISNULL(#nationality, nationality)
WHERE person_id_guid = #person_id
IF #account_id IS NOT NULL
BEGIN
UPDATE dbo.person_reference
SET account_id = #account_id
,proc_date_time = GETDATE()
WHERE person_id = #person_id
END
END
ELSE
BEGIN
SELECT 'Student does not exist, creating'
--INSERT person
SET #person_id = NEWID()
INSERT INTO dbo.person (
person_id_guid
,prefix_honorific
,first_name
,middle_name
,surname
,familiar_name
,date_of_birth
,external_email_address
,mobile_phone_no
,gender
,ethnicity
,domicile
,disability
,nationality
,telephone_no
,prev_surname
,country_of_birth
,source_system
,proc_date_time
)
VALUES (
#person_id
,#prefix_honorific
,#first_name
,#middle_name
,#surname
,#familiar_name
,#date_of_birth
,#external_email_address
,#mobile_phone_no
,#gender
,#ethnicity
,#domicile
,#disability
,#nationality
,#telephone_no
,#prev_surname
,'OneUni'
,#country_of_birth
,GETDATE()
)
--INSERT person_reference
INSERT INTO dbo.person_reference (
person_id
,student_id
,proc_date_time
,account_id
)
VALUES (
#person_id
,#student_id
,GETDATE()
,#account_id
)
END
-- Create/Update student
IF EXISTS ( SELECT account_id FROM dbo.student WITH (NOLOCK) WHERE account_id = #account_id and ISNULL(status,'') <> 'Delete')
BEGIN
SELECT 'Student exists, updating'
UPDATE student
SET
--account_id = #account_id
--,student_id = #student_id
ucas_id = #UCAS_ID
,unique_learner_number = #uln
,main_programme_code = isnull(#main_programme_code, main_programme_code)
,student_email_address = #student_email_address
,currently_studying_flag = #currently_studying_flag
,hesa_student_id = #HesaStudentID
,visa_required = #VisaReq
,cas_status = #cas_status
,student_status = #student_status
,purpose_of_residency = #PurposeOfResidency
,mod_date_time = GETDATE()
,student_support_no = #student_support_no
,source_system = #source_system
,exam_id = #exam_id
,su_opt = #su_opt
,international_sponsored_students = #international_sponsored_students
,visa_type = #visa_type
WHERE account_id = #account_id
END
-- Create Student/Student Programme/Student Module
ELSE
BEGIN
SELECT 'Student does not exist, creating'
SET #person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = #account_id and ISNULL(status,'') <> 'Delete')
INSERT INTO student (
person_id_guid
,account_id
,ucas_id
,unique_learner_number
,student_email_address
,currently_studying_flag
,hesa_student_id
,visa_required
,cas_status
,student_status
,purpose_of_residency
,proc_date_time
,source_system
,main_programme_code
,student_id
,student_support_no
,exam_id
,su_opt
,international_sponsored_students
,visa_type
)
VALUES (
#person_id
,#account_id
,#UCAS_ID
,#uln
,#student_email_address
,#currently_studying_flag
,#HesaStudentID
,#VisaReq
,#cas_status
,#student_status
,#PurposeOfResidency
,getdate()
,#source_system
,#main_programme_code
,#student_id
,#student_support_no
,#exam_id
,#su_opt
,#international_sponsored_students
,#visa_type
)
END
-- Create/Update student_programmes if change_record is 'Course'
IF #change_type = 'Programme'
BEGIN
-- Create/Update student_programmes
IF EXISTS ( SELECT student_id FROM student_programmes WITH (NOLOCK) WHERE account_id = #account_id and programme_code = #programme_code)
BEGIN
SELECT 'Student Programme exists, updating'
--UPDATE student_programme? (Wait for confirmation)
UPDATE student_programmes
SET
--account_id = #account_id
--,student_id = #student_id
--course_code = #course_code
programme_description = #programme_description
,[start_date] = #start_date
,end_date = #end_date
,mdh_stage_code = #mdh_stage_code
,main_award_flag = #main_award_flag
,load_category = #load_category
,qualification_level = #qualification_level
,student_study_level = #student_study_level
,is_wbdl = #is_wbdl
,school_code = #school_code
,college_code = #college_code
,campus_code = #campus_code
,ul_qual_aim = #ul_qual_aim
,ul_qual_aim_description = #ul_qual_aim_desc
,mod_date_time = GETDATE()
WHERE account_id = #account_id
and programme_code = #programme_code
END
ELSE
BEGIN
SELECT 'Student Programme does not exist, creating'
SET #person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = #account_id and ISNULL(status,'') <> 'Delete')
--INSERT student_programme
INSERT INTO student_programmes (
person_id_guid
,account_id
,student_id
,programme_code
,programme_description
,[start_date]
,end_date
,mdh_stage_code
,main_award_flag
,load_category
,qualification_level
,student_study_level
,is_wbdl
,school_code
,college_code
,campus_code
,ul_qual_aim
,ul_qual_aim_description
,mod_date_time
)
VALUES (
#person_id
,#account_id
,#student_id
,#programme_code
,#programme_description
,#start_date
,#end_date
,#mdh_stage_code
,#main_award_flag
,#load_category
,#qualification_level
,#student_study_level
,#is_wbdl
,#school_code
,#college_code
,#campus_code
,#ul_qual_aim
,#ul_qual_aim_desc
,GETDATE()
)
END
END
-- Create/Update student_modules if change_record is 'Module'
IF #change_type = 'Module'
BEGIN
IF EXISTS ( SELECT student_id FROM student_modules WITH (NOLOCK) WHERE account_id = #account_id and programme_code = #programme_code and module_code = #module_code)
BEGIN
SELECT 'Student Module exists, updating'
--UPDATE student_module? (Wait for confirmation)
UPDATE student_modules
SET
--account_id = #account_id
--,student_id = #student_id
--course_code = #course_code
--module_code = #module_code
module_description = #module_desc
,mdh_stage_code = #mdh_stage_code
,student_study_level = #student_study_level
,mod_date_time = GETDATE()
WHERE account_id = #account_id
and programme_code = #programme_code
and module_code = #module_code
END
ELSE
BEGIN
SELECT 'Student Module does not exist, creating'
SET #person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = #account_id and ISNULL(status,'') <> 'Delete')
-- If the programme for the module/student doesnt exist, insert it
IF NOT EXISTS ( SELECT student_id FROM dbo.student_programmes WITH (NOLOCK) WHERE account_id = #account_id and programme_code = #programme_code)
BEGIN
SET #person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = #account_id )
--INSERT student_programme
INSERT INTO student_programmes (
person_id_guid
,account_id
,student_id
,programme_code
,programme_description
,[start_date]
,end_date
,mdh_stage_code
,main_award_flag
,load_category
,qualification_level
,student_study_level
,is_wbdl
,school_code
,college_code
,campus_code
,ul_qual_aim
,ul_qual_aim_description
,mod_date_time
)
VALUES (
#person_id
,#account_id
,#student_id
,#programme_code
,#programme_description
,#start_date
,#end_date
,#mdh_stage_code
,#main_award_flag
,#load_category
,#qualification_level
,#student_study_level
,#is_wbdl
,#school_code
,#college_code
,#campus_code
,#ul_qual_aim
,#ul_qual_aim_desc
,GETDATE()
)
END
--INSERT student_module
INSERT INTO student_modules (
person_id_guid
,account_id
,student_id
,programme_code
,module_code
,module_description
,mdh_stage_code
,student_study_level
,mod_date_time
)
VALUES (
#person_id
,#account_id
,#student_id
,#programme_code
,#module_code
,#module_desc
,#mdh_stage_code
,#student_study_level
,GETDATE()
)
END
END
END

Sql Scope_Identity() return Null?

I am facing this issue that my stored proc always return NULL though i have set my #output variable as well.I want to get last inserted scope Id from the table.
can someone help me where i have got wrong?
ALTER PROC spAddOrUpdateMember
#pMemberId INT = 0 ,
#pFirstName VARCHAR(50) = 'aa',
#pLastName VARCHAR(50)='aa' ,
#pMemberCode VARCHAR(15) = '12312',
#pDOB DATE = '03/10/2019',
#pGrade INT = 2 ,
#pCNIC VARCHAR(14) = '3423434',
#pFatherName VARCHAR(50) = 'asdasd' ,
#pCurrentAddress VARCHAR(MAX) = 'asds' ,
#pPermanentAddress VARCHAR(MAX) = 'fgdf',
#pEmploymentAddress VARCHAR(MAX) = 'ytuyu' ,
#pNationality INT =2
#output int = 0 output
AS
BEGIN
IF #pMemberId > 0
BEGIN
---UPDATE ME
UPDATE [dbo].[QC_Member_Profile]
SET
[FirstName] = #pFirstName
,[LastName] = #pLastName
,[DOB] = #pDOB
,[CNIC] = #pCNIC
,[FatherName] = #pFatherName
,[CurrentAddress] = #pCurrentAddress
,[PermanentAddress] = #pPermanentAddress
,[Nationality] = #pNationality
,[MemberTypeId] =#pMemberTypeId
WHERE MemberId = #pMemberId
END
ELSE
BEGIN
---INSERT ME
INSERT INTO QC_Member_Profile VALUES(
dbo.PIdentityKey(0),
#pFirstName,
#pLastName,
#pDOB,
#pCNIC,
#pFatherName,
#pCurrentAddress,
#pPermanentAddress,
#pNationality,
)
set #output = SCOPE_IDENTITY();
SELECT #output = SCOPE_IDENTITY();
select #output
END
END
I've guessed the name of your ID column, however, this should work. You'll need to amend the name of your ID column if it isn't called MemberID or if it doesn't have the data type int:
ALTER PROC spAddOrUpdateMember #pMemberId int = 0,
#pFirstName varchar(50) = 'aa',
#pLastName varchar(50) = 'aa',
#pMemberCode varchar(15) = '12312',
#pDOB date = '03/10/2019',
#pGrade int = 2,
#pCNIC varchar(14) = '3423434',
#pFatherName varchar(50) = 'asdasd',
#pCurrentAddress varchar(MAX) = 'asds',
#pPermanentAddress varchar(MAX) = 'fgdf',
#pEmploymentAddress varchar(MAX) = 'ytuyu',
#pNationality int = 2,
#output int = 0 OUTPUT
AS
BEGIN
IF #pMemberId > 0
BEGIN
UPDATE [dbo].[QC_Member_Profile]
SET [FirstName] = #pFirstName,
[LastName] = #pLastName,
[DOB] = #pDOB,
[CNIC] = #pCNIC,
[FatherName] = #pFatherName,
[CurrentAddress] = #pCurrentAddress,
[PermanentAddress] = #pPermanentAddress,
[Nationality] = #pNationality,
[MemberTypeId] = #pMemberTypeId
WHERE MemberId = #pMemberId;
END;
ELSE
BEGIN
DECLARE #ins table (OutputID int);
INSERT INTO QC_Member_Profile
OUTPUT Inserted.MemberID --guessed name
INTO #Ins
VALUES (dbo.PIdentityKey(0), #pFirstName, #pLastName, #pDOB, #pCNIC, #pFatherName, #pCurrentAddress, #pPermanentAddress, #pNationality);
SELECT #output = OutputID
FROM #ins;
SELECT #Output;
END;
END;
I've also fixed the syntax errors that were in your original question.

Using case to delete data row

I created a simple stored procedure to update my table, but currently what I need is
Delete * from [ABCSystem].[dbo].[NEW_TEST_NUMBER]
WHERE sONbr = #sONbr AND SOLine = #SOLine
If #Statuscode is = "N001" by using case. Anyone can show me a simple sample or something?
Thank you.
ALTER PROCEDURE [dbo].[usp_Testing]
-- Add the parameters for the stored procedure here
#sONbr nvarchar(50) = NULL,
#SOLine nvarchar(50) = NULL,
#SerialNbr nvarchar(50) = NULL,
#StatusCode nvarchar(50) = NULL,
#PackType nvarchar(50) = NULL,
#PalletID nvarchar(50) = NULL,
#PackingListNo nvarchar(50) = NULL,
#CrDateTime nvarchar(50) = NULL,
#CrUserID nvarchar(50) = NULL,
#return nvarchar(50) = NULL OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
IF EXISTS(
SELECT sONbr , SOLine
FROM [ABCSystem].[dbo].[NEW_TEST_NUMBER]
WHERE sONbr = #sONbr AND SOLine = #SOLine
)
BEGIN
UPDATE [ABCSystem].[dbo].[NEW_TEST_NUMBER]
SET StatusCode = #StatusCode
,LastUpdDateTime = GETDATE()
,LastUpdUserID = #CrUserID
,StatusDesc =
CASE #StatusCode WHEN 'N001' THEN 'New'
WHEN 'PR001' THEN 'Prepack In Progress'
WHEN 'PR002' THEN 'PrePacking Completed'
WHEN 'WE002' THEN 'Weight Complete'
END
,PalletID =
CASE #StatusCode WHEN 'N001' THEN cast(null as nvarchar(50))
ELSE PalletID
END
,PackType =
CASE #StatusCode WHEN 'N001' THEN cast(null as nvarchar(50))
ELSE PackType
END
,JobID =
CASE #StatusCode WHEN 'N001' THEN cast(null as nvarchar(50))
ELSE JobID
END
,JobCrDateTime =
CASE #StatusCode WHEN 'N001' THEN cast(null as nvarchar(50))
ELSE JobCrDateTime
END
,PackingListNo =
CASE #StatusCode WHEN 'N001' THEN cast(null as nvarchar(50))
ELSE PackingListNo
END
WHERE sONbr = #sONbr AND SOLine = #SOLine
IF ##ERROR <> 0
Set #Return = 'UPDATE FAILED!'
ELSE
Set #Return = 'UPDATE SUCCESSFULLY.'
END
ELSE
BEGIN
Set #Return = 'NO DATA EXIST!'
END
As far as I am seing your problem, you have to create a simple table that will tell you INSERT, UPDATE, DELETE. Link the table inside your stored proc. This will help you to sort things down.
OR
Simply sort everything in loop inside stored proc telling that
WHEN #StatusCode = 'N001'
DO update
ELSE
DO something else

How to run stored procedure query between 2 table & insert data base on reference column?

I've created a simple stored procedure to update my Sample_TAG_NUMBERTEST table but I need it to get an ItemCode from another table PartItem by using both SOLine & SONbr to do the query but I don't know how to write it.
2nd thing I need to do is when my StatusCode column is N001 then my DESC column will automatically insert NEW ORDER data value, for PR002 my DESC column will become Progress.
How can I do that? Thanks in advance.
ALTER PROCEDURE [dbo].[SampleTagNumberUpdate]
#sONbr nvarchar(50) = NULL,
#SOLine nvarchar(50) = NULL,
#SerialNbr nvarchar(50) = NULL,
#StatusCode nvarchar(50) = NULL,
#PackType nvarchar(50) = NULL,
#PalletID nvarchar(50) = NULL,
#PackingListNo nvarchar(50) = NULL,
#ItemCode nvarchar(50) = NULL,
#CrDateTime nvarchar(50) = NULL,
#CrUserID nvarchar(50) = NULL,
#return nvarchar(50) = NULL OUTPUT
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS(SELECT sONbr , SOLine
FROM [SampleSystem].[dbo].[Sample_TAG_NUMBERTEST]
WHERE sONbr = #sONbr AND SOLine = #SOLine)
BEGIN
UPDATE [SampleSystem].[dbo].[Sample_TAG_NUMBERTEST]
SET SerialNbr = #SerialNbr
,StatusCode = #StatusCode
,PackType = #PackType
,PalletID = #PalletID
,PackingListNo = #PackingListNo
,ItemCode = #ItemCode
,LastUpdDateTime = GETDATE()
,LastUpdUserID = #CrUserID
WHERE sONbr = #sONbr AND SOLine = #SOLine
IF ##ERROR <> 0
Set #Return = 'UPDATE FAILED'
ELSE
Set #Return = 'UPDATE SUCCESSFULLY'
END
ELSE
BEGIN
INSERT INTO [SampleSystem].[dbo].[Sample_TAG_NUMBERTEST](SONbr, SOLine, SerialNbr
,StatusCode
,PackType
,PalletID
,PackingListNo
,ItemCode
,CrDateTime
,CrUserID)
VALUES(#sONbr, #SOLine, #SerialNbr, #StatusCode, #PackType
,#PalletID
,#PackingListNo
,#ItemCode
,GETDATE()
,#CrUserID)
IF ##ERROR <> 0
Set #Return = 'INSERT DATA FAILED'
ELSE
Set #Return = 'INSERT DATA SUCCESSFULLY'
END
END
I believe I have provided an example for both questions below:
UPDATE T
SET ItemCode = (SELECT PI.ItemCode FROM PartItem AS PI WHERE PI.SO = #SO AND PI.SO_LINE = #SO_LINE),
DESC = CASE #StatusCode WHEN 'N001' THEN 'NEW ORDER'
ELSE CASE #StatusCode WHEN 'PR002' THEN 'PROGRESS' ELSE '' END
END,
<other columns here...>
FROM SAMPLE_TAG_NUMBERTEST AS T

An object or column name is missing or empty

I am getting the following error
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name.
for the query show below:
CREATE PROC [dbo].[Sp_Table1] #ctlg_ipt_event_id int
AS
SET NOCOUNT ON
DECLARE #current_status NCHAR(1), #ready_status_code NCHAR(1)
DECLARE #current_action NCHAR(1), #ready_action_code NCHAR(1), #done_action_code NCHAR(1)
DECLARE #pst_user_id int
SELECT #current_status = status_code
,#current_action = action_code
,#pst_user_id = last_mod_user_id
FROM merch_ctlg_ipt_event
WHERE ctlg_ipt_event_id = #ctlg_ipt_event_id
Select #ready_status_code = 'o'
, #ready_action_code = 'a'
, #done_action_code = 'b'
IF #current_status <> #ready_status_code OR #current_action <> #ready_action_code
BEGIN
RETURN
END
BEGIN TRAN
declare #rows int
,#err int
,#i int
,#name nvarchar(50) --COLLATE SQL_AltDiction_Pref_CP850_CI_AS
,#resolved_View_Name_category_id int
,#xref_value int
,#availability_start_date datetime
,#availability_end_date datetime
,#status_code int
,#last_mod_user_id int
,#CT datetime
,#supplier_id int
,#View_Name_id int
select #i = 1
,#CT = current_timestamp
Select Distinct mc.name,
mc.resolved_View_Name_category_id,
mc.xref_value,
mc.availability_start_date,
mc.availability_end_date,
mc.status_code,
CASE WHEN mc.last_mod_user_id = 42
THEN #pst_user_id
ELSE mc.last_mod_user_id
END as last_mod_user_id,
CURRENT_tsp
,IDENTITY(int,1,1) as rn
,si.supplier_id
,si.View_Name_id
into #temp
FROM View_Name AS si
JOIN merch_ctlg_ipt_View_Name AS mc
ON mc.supplier_id = si.supplier_id
AND mc.resolved_View_Name_id = si.View_Name_id
AND mc.cat_imp_event_id = #ctlg_ipt_event_id
AND mc.accept_flag = 'y'
WHERE si.shipper_flag = 'n'
select #rows=##ROWCOUNT,#err=##error
if #rows > 0 and #err=0
Begin
While #i <=#rows
begin
SElect #name = name,
#resolved_View_Name_category_id = resolved_View_Name_category_id,
#xref_value = xref_value,
#availability_start_date = availability_start_date,
#availability_end_date = availability_end_date,
#status_code = mc.status_code,
#last_mod_user_id =last_mod_user_id ,
,#i=#i+1
,#supplier_id=supplier_id
,#View_Name_id=View_Name_id
from #temp
Where rn=#i
UPDATE View_Name
SET name = #name,
View_Name_category_id = #resolved_View_Name_category_id,
xref_value = #xref_value,
availability_start_date = #availability_start_date,
availability_end_date = #availability_end_date,
status_code = #status_code,
last_mod_user_id = #last_mod_user_id ,
last_mod_timestamp = #CT
Where #sup_id = supplier_id
AND #View_Name_id = View_Name_id
AND shipper_flag = 'n'
IF ##ERROR > 0
BEGIN
ROLLBACK TRAN
RETURN
END
End
End
UPDATE
merch_ctlg_ipt_event
SET action_code = #done_action_code,
last_mod_timestamp = #CT
WHERE ctlg_ipt_event_id = #ctlg_ipt_event_id
IF ##ERROR > 0
BEGIN
ROLLBACK TRAN
RETURN
END
COMMIT TRAN
Return
go
Could you please help ?
You have 2 commas in a row here
#last_mod_user_id =last_mod_user_id ,
,#i=#i+1
Also probably not relevant to the error message but you have a line
Where #sup_id = supplier_id
but the declared variable is #supplier_id