How to solve "Self referencing table conflicted with the FOREIGN KEY constraint" - sql

I am creating a simple table with automatically generated employee id with a prefix like
sale_900000,sale_900001,sale_900002,sale_900003
It is a self-referencing table.
When I insert data into the table I get errors like this:
*Msg 547, Level 16, State 0, Procedure tr_generate_emp_id, Line 42
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "Registration_Registration". The conflict occurred in database "test", table "dbo.Registration", column 'empid'.*
For automatic employee id I am using an instead of insert trigger on the table
This is my table
CREATE TABLE [dbo].[Registration](
[empid] [varchar](40) NOT NULL,
[id] [int] IDENTITY(900000,1) NOT NULL,
[First_Name] [varchar](40) NULL,
[Last_Name] [varchar](40) NULL,
[Address] [varchar](40) NULL,
[E_Mail] [varchar](40) NULL,
[Country] [varchar](40) NULL,
[Mobile_No] [varchar](40) NULL,
[Designation] [varchar](40) NULL,
[managerID] [varchar](40) NULL,
CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED
(
[empid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[Registration] WITH CHECK ADD CONSTRAINT [Registration_Registration] FOREIGN KEY([managerID])
REFERENCES [dbo].[Registration] ([empid])
and this is my trigger
ALTER TRIGGER [dbo].[tr_generate_emp_id] ON [dbo].[Registration]
INSTEAD OF INSERT
AS
BEGIN
DECLARE #id INT
DECLARE #id1 INT
DECLARE #id_s VARCHAR(50)
DECLARE #empid VARCHAR(50)
DECLARE #First_Name VARCHAR(50)
DECLARE #Last_Name VARCHAR(50)
DECLARE #Address VARCHAR(50)
DECLARE #E_Mail VARCHAR(50)
DECLARE #Country VARCHAR(50)
DECLARE #Mobile_No VARCHAR(50)
DECLARE #Designation VARCHAR(50)
DECLARE #managerID VARCHAR(50)
SELECT #id = Id
FROM dbo.Registration
IF #id IS NULL
SET #id = 899999
SET #id1 = #id + 1
SELECt #First_Name = First_Name ,
#Last_Name = Last_Name ,
#Address = Address ,
#E_Mail = E_Mail ,
#Country = Country ,
#Mobile_No = Mobile_No ,
#Designation = Designation ,
#managerID = managerID
FROM INSERTED
SET #id_s = CONVERT(VARCHAR(50), #id1)
SET #empid = 'Sale_' + #id_s
INSERT INTO dbo.Registration
( empid ,
First_Name ,
Last_Name ,
Address ,
E_Mail ,
Country ,
Mobile_No ,
Designation ,
managerID
)
VALUES ( #empid , -- empid - varchar(40)
#First_Name , -- First_Name - varchar(40)
#Last_Name , -- Last_Name - varchar(40)
#Address , -- Address - varchar(40)
#E_Mail , -- E_Mail - varchar(40)
#Country, -- Country - varchar(40)
#Mobile_No , -- Mobile_No - varchar(40)
#Designation , -- Designation - varchar(40)
#managerID -- managerID - varchar(40)
)
END
i am inserting
INSERT INTO dbo.Registration
( empid ,
First_Name ,
Last_Name ,
Address ,
E_Mail ,
Country ,
Mobile_No ,
Designation ,
managerID
)
VALUES ( '' , -- empid - varchar(40)
'admin' , -- First_Name - varchar(40)
'jon' , -- Last_Name - varchar(40)
's-24' , -- Address - varchar(40)
'abc#gmail.com' , -- E_Mail - varchar(40)
'india' , -- Country - varchar(40)
'098735322211' , -- Mobile_No - varchar(40)
'manager' , -- Designation - varchar(40)
'' -- managerID - varchar(40)
)
and error
Msg 547, Level 16, State 0, Procedure tr_generate_emp_id, Line 42
The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "Registration_Registration". The conflict occurred in database "test", table "dbo.Registration", column 'empid'.
The statement has been terminated.

The main issue is that you're doing this in the first place. I just read through your trigger. Just changed empid to be a computed field...
CREATE TABLE [dbo].[Registration](
[empid] AS ('Sale_' + Convert(Varchar(50),id)) PERSISTED,
[id] [int] IDENTITY(900000,1) NOT NULL,
[First_Name] [varchar](40) NULL,
[Last_Name] [varchar](40) NULL,
[Address] [varchar](40) NULL,
[E_Mail] [varchar](40) NULL,
[Country] [varchar](40) NULL,
[Mobile_No] [varchar](40) NULL,
[Designation] [varchar](40) NULL,
[managerID] [varchar](40) NULL,
CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED
(
[empid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

In your trigger I think this line
SELECT #id = Id
is meant to read
SELECT #id = MAX(Id)
I'm surprised you havent received PK violation errors as a result. The FK violation error can only be due to trying to insert MangerId value that doesn't match an empid value (as far as I can see from the code). So check what managerId value you are supplying.

Check your Nest Level.
http://msdn.microsoft.com/en-us/library/ms182737(v=sql.100).aspx
I would put this value in a (dbo.) AuditTable, something that has nothing to do with your primary table.
trigger_nestlevel()
Change from Row Based to Set Based Logic.

I re-did your trigger to be set based but I think #Love2Learn may have a better solution for you.
ALTER TRIGGER [dbo].[tr_generate_emp_id] ON [dbo].[Registration]
INSTEAD OF INSERT
AS
BEGIN
DECLARE #id INT
-- Get the last id used.
SELECT #id = MAX(Id)
FROM dbo.Registration
IF #id IS NULL
SET #id = 899999
WITH MyInserted AS (SELECT
ROW_NUMBER() OVER(ORDER BY E_Mail) AS rownum-- Email is just a random order by here. Could be anything you want.
,*
FROM inserted)
INSERT INTO dbo.Registration
( empid ,
First_Name ,
Last_Name ,
Address ,
E_Mail ,
Country ,
Mobile_No ,
Designation ,
managerID
)
SELECT -- add the last id used with the rownum (ranges from 1 to num of rows)
'Sale_'+CONVERT(VARCHAR(50), #id+rownum) -- empid - varchar(40)
First_Name, -- First_Name - varchar(40)
Last_Name, -- Last_Name - varchar(40)
Address, -- Address - varchar(40)
E_Mail, -- E_Mail - varchar(40)
Country, -- Country - varchar(40)
Mobile_No, -- Mobile_No - varchar(40)
Designation, -- Designation - varchar(40)
ManagerID -- managerID - varchar(40)
FROM MyInserted
END

Related

Trying to write a stored procedure with a Identity column to create my table values

I am trying to write a stored procedure that creates a new row of data in my table that has an Identity column. I can't figure out what I'm doing wrong to get it to store my data in the table.
Here is my table
CREATE TABLE [Promotional].[Proposals]
(
[Proposal_Uid] [int] IDENTITY(1,1) NOT NULL,
[Prime_Contract] [nvarchar](30) NULL,
[Sub_Contract] [nvarchar](30) NULL,
[Po_Id] [nvarchar](30) NULL,
[Proposal_Title] [nvarchar](50) NULL,
[Client_Name] [nvarchar](40) NULL,
[Client_Code] [nvarchar](20) NULL,
[Total_Proposal_Amount] [decimal](18, 2) NULL,
[ODC_Amount] [decimal](18, 2) NULL,
[Manager_Name] [nvarchar](30) NULL,
[Admin] [nvarchar](30) NULL,
[Due_Date] [date] NULL,
[Start_Date] [date] NULL,
[End_Date] [date] NULL,
[Proposal_Number] [nvarchar](20) NULL,
[Contract_Type] [nvarchar](16) NULL,
CONSTRAINT [Proposal_Uid]
PRIMARY KEY CLUSTERED ([Proposal_Uid] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Here is my stored procedure:
ALTER PROCEDURE [Promotional].[Proposals_Create]
(#Prime_Contract nvarchar(30),
#Sub_Contract nvarchar(30),
#Po_Id nvarchar(30),
#Proposal_Title nvarchar(50),
#Client_Name nvarchar(40),
#Client_Code nvarchar(20),
#Total_Proposal_Amount decimal(18,2),
#ODC_Amount decimal(18,2),
#Manager_Name nvarchar(30),
#Admin nvarchar(30),
#Due_Date date,
#Start_Date date,
#End_Date date,
#Proposal_Number nvarchar(20),
#Contract_Type nvarchar(16),
#NewId int output)
AS
BEGIN
IF #NewId IS NULL
BEGIN
INSERT INTO Promotional.Proposals (Prime_Contract, Sub_Contract, Po_Id, Proposal_Title, Client_Name, Client_Code,
Total_Proposal_Amount, ODC_Amount, Manager_Name, Admin, Due_Date, Start_Date, End_Date, Proposal_Number, Contract_Type)
VALUES (#Prime_Contract, #Sub_Contract, #Po_Id, #Proposal_Number, #Client_Name, #Client_Code,
#Total_Proposal_Amount, #ODC_Amount, #Manager_Name, #Admin, #Due_Date, #Start_Date, #End_Date,
#Proposal_Number, #Contract_Type)
SET #NewId = SCOPE_IDENTITY()
END
RETURN #NewId
END
GO
I know it's something small I missed. But, my OUTCOME is that when you run the procedure, it inserts the values in the correct columns, and the primary key is auto updated without having to stick your own value into the statement. My execution of the statement would be as follows
EXEC Promotional.Proposals_Create 'hj','fd','fd','fd','fd','fd','0.23','1.24','fd','fd','2020/08/30','2020/08/30','2020/08/30','fd','fd';
And I get this error:
Msg 201, Level 16, State 4, Procedure Promotional.Proposals_Create, Line 0 [Batch Start Line 0]
Procedure or function 'Proposals_Create' expects parameter '#NewId', which was not supplied.
I shouldn't have to provide that parameter because it should auto update with my PK which is Proposal_Uid. Or do I have to make my Proposal_Uid the value returned from ScopeIdentity?
#NewID should not be a parameter of the procedure since it is an IDENTITY column.
You can get the stored procedure to return the generated id by providing an output variable to store its content. This behaviour is also documented here.
declare #test int;
EXEC Proposals_Create 'hj','fd','fd','fd','fd','fd','0.23','1.24','fd','fd',
'2020/08/30','2020/08/30','2020/08/30','fd','fd', #test OUTPUT;
select #test as 'new_identity';
set #test = null; -- because of the IF statement in your procedure...
EXEC Proposals_Create 'hj','fd','fd','fd','fd','fd','0.23','1.24','fd','fd',
'2020/08/30','2020/08/30','2020/08/30','fd','fd', #test OUTPUT;
select #test as 'new_identity';
The selects return 1 and 2 respectively.
Fiddle (Remark: schema name removed in code above and in fiddle).

there is already an object named '' in the database

This is my code:
CREATE TABLE supplier -- creating table supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
I get the the error:
Msg 2714, Level 16, State 6, Line 34
There is already an object named 'supplier' in the database.
Any help? Thanks!
Please try this code.
IF EXISTS(SELECT 1 FROM sys.tables WHERE name = 'supplier')
DROP TABLE dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
You need to check if the table exists first
IF OBJECT_ID('dbo.supplier', 'U') IS NOT NULL
DROP TABLE dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO
If you are using 2016+ you can use
DROP TABLE IF EXISTS dbo.supplier;
CREATE TABLE dbo.supplier
(
supplierID INT NOT NULL IDENTITY,
supplierName VARCHAR(30) NOT NULL,
suppplierNo VARCHAR(10) NOT NULL,
supplierEmail VARCHAR(30) NOT NULL,
CONSTRAINT PK_supplierID PRIMARY KEY(supplierID)
)
GO

Log table using trigger

I need to create trigger which insert into dbo.Log table values:
- in action_type : how many records was UPDATED ,DELETED or INSERTED
- in datetime_of_action ofcourse timestamp of action
For now I have this:
DROP TABLE dbo.Log
CREATE TABLE dbo.Log (
logid INT NOT NULL IDENTITY,
action_type NVARCHAR(50) NOT NULL,
datetime_of_action DATETIME NOT NULL,
CONSTRAINT PK_Log PRIMARY KEY(logid));
CREATE TRIGGER trig2
ON Sales.Customers
FOR UPDATE , DELETE, INSERT
AS
BEGIN
......................
END
SELECT * FROM dbo.Log
And this is a script of Sales.Customers table:
CREATE TABLE [Sales].[Customers](
[custid] [int] IDENTITY(1,1) NOT NULL,
[companyname] [nvarchar](40) NOT NULL,
[contactname] [nvarchar](30) NOT NULL,
[contacttitle] [nvarchar](30) NOT NULL,
[address] [nvarchar](60) NOT NULL,
[city] [nvarchar](15) NOT NULL,
[region] [nvarchar](15) NULL,
[postalcode] [nvarchar](10) NULL,
[country] [nvarchar](15) NOT NULL,
[phone] [nvarchar](24) NOT NULL,
[fax] [nvarchar](24) NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
(
[custid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
If anyone knows how do this I am open on your advice.
Use this query:
CREATE TRIGGER trig2
ON Sales.Customers
AFTER UPDATE , DELETE, INSERT
AS
BEGIN
IF EXISTS (select * From inserted)
BEGIN
IF EXISTS (select * From deleted)
BEGIN
INSERT INTO dbo.Log (action_typ, datetime_of_action )
VALUES(CAST(SELECT COUNT(*) FROM deleted) as VARCHAR(50) + ' records have been updated',GETDATE())
END
ELSE
BEGIN
INSERT INTO dbo.Log (action_typ, TOTAL, datetime_of_action )
VALUES(CAST(SELECT COUNT(*) FROM inserted) as VARCHAR(50) + ' records have been iniserted',GETDATE())
END
ELSE
BEGIN
INSERT INTO dbo.Log (action_typ, TOTAL, datetime_of_action )
VALUES(CAST(SELECT COUNT(*) FROM deleted) as VARCHAR(50) + ' records have been deleted',GETDATE())
END
END
CREATE TRIGGER trig2
ON Sales.Customers
FOR UPDATE , DELETE, INSERT
AS
BEGIN
SET NOCOUNT ON;
-- First determine the action
DECLARE #Action NVARCHAR(50);
SET #Action = (CASE WHEN EXISTS(SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
THEN N'Update: ' + CAST( (SELECT COUNT(*) FROM INSERTED) AS NVARCHAR(10))
WHEN EXISTS(SELECT * FROM INSERTED) AND NOT EXISTS(SELECT * FROM DELETED)
THEN N'Insert: ' + CAST( (SELECT COUNT(*) FROM INSERTED) AS NVARCHAR(10))
WHEN EXISTS(SELECT * FROM DELETED) AND NOT EXISTS(SELECT * FROM INSERTED)
THEN N'Delete: ' + CAST( (SELECT COUNT(*) FROM DELETED) AS NVARCHAR(10))
ELSE NULL -- Skip. It may have been a "failed delete".
END)
INSERT INTO dbo.[Log] (action_type, datetime_of_action)
VALUES (#Action , GETDATE());
END

Sql stored procedure taking lot of time for execution

I am facing a big issue, the below stored procedure taking lot of time for execution. Please help me to find issues with following stored procedure .
We need to insert bulk subscriber list from excel in to database. But only 60 subscribers are getting inserted in to db in one minute.
Please help me to resolve the issue.
USE [SMS]
GO
/****** Object: StoredProcedure [dbo].[SP_ProcessFile] Script Date: 01/30/2015 12:56:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_ProcessFile]
#JobCode varchar(25)
WITH RECOMPILE
AS
declare
#jobCode1 Varchar(50),
#count int,#Code varchar(50),#Name [varchar](50),#Date Datetime,#Status int,
#i int,#EUCount int,#SubCount int,
#Add1 nvarchar(3000) ,
#Add2 nvarchar(500) ,
#Add3 nvarchar(500)
,#refdate [varchar](50) ,
#reference [varchar](50) ,
#Joined [varchar](50),
#Joinmonth [nvarchar](50),
#Activated [varchar](50),
#ActivMonth [nvarchar](50),
#Center [varchar](50) ,
#Region [varchar](50) ,
#Area [varchar](50) ,
#Modem [varchar](50) ,
#Adomstatus [varchar](50)
, #AddCode [varchar](50)
set #i = 1
Set #jobCode1 =#JobCode
BEGIN
SET NOCOUNT ON
Set #Status = (Select Distinct(status) from TSMST1005 where Jobcode = #jobCode1)
if (#Status = 0)
begin
Select '1' as res
end
else
begin
CREATE TABLE #tblSMS(pID int identity(1,1),
[Reference] [nvarchar](50) NULL,
[Date] [varchar](50) NOT NULL,
[Code] [nvarchar](50) NULL,
[Subname] [varchar](500) NULL,
[Address1] [nvarchar](3000) NULL,
[Address2] [nvarchar](500) NULL,
[Address3] [nvarchar](500) NULL,
[Joined] [varchar](50) NULL,
[Joinmonth] [varchar](50) NULL,
[Activated] [varchar](50) NULL,
[ActivMonth] [nvarchar](50) NULL,
[Center] [varchar](50) NULL,
[Region] [varchar](50) NULL,
[Area] [varchar](50) NULL,
[Modem] [varchar](50) NULL,
[Adomstatus] [varchar](50) NULL,
[RefDate] [varchar](50) NOT NULL)
insert into #tblSMS
SELECT Reference,[Date],
Code,Subname ,Address1 ,Address2 ,Address3 ,
Joined , Joinmonth ,Activated, ActivMonth ,
Center,Region,Area,Modem,Adomstatus ,refdate FROM TSMST1005 where jobcode = #jobCode1 and Status =1
WHILE #i <= (SELECT COUNT(*) FROM #tblSMS)
BEGIN
SELECT
#Code =Code,
#Name = Subname,
#Date =[Date],
#Add1 =Address1 ,
#Add2 =Address2 ,
#Add3= Address3,
#reference =Reference ,
#Joined = Joined,
#Joinmonth =Joinmonth,
#Activated =Activated,
#ActivMonth =ActivMonth,
#Center = Center,
#Region = Region,
#Area= Area,
#Modem = Modem ,
#Adomstatus =Adomstatus,
#refdate = RefDate
From #tblSMS where pID = #i
Insert into TCMST5001 (CompanyCode , Address1,Address2 ,Address3 ,CreatedDate ,Status) values('001',#Add1 ,#Add2,#Add3,GETDATE(),1)
Set #count = SCOPE_IDENTITY()
Set #AddCode = 'ADD' + Cast(#count As Varchar(10))
Update TCMST5001 Set Code =#AddCode Where AddressID =#count
Set #EUCount = (Select COUNT(*) from TCCOM0005 where EnterpriseUnitCode = #Center)
if (#EUCount = 0)
Begin
Insert into TCCOM0005(AddressCode,CompanyCode,EnterpriseUnitCode,EnterpriseUnitName,Status) values(#count ,'001',#Center,#Center ,1)
END
Set #SubCount = (Select COUNT(*) from TSMST1001 where Subscriber = #Code)
if (#SubCount =0)
begin
Insert into TSMST1001(ActivationDate ,refdate , Address ,AlternateName ,Area ,Region ,Subscriber,Name ,date ,CreatedDate ,EnterpriseUnit ,Status)
values(#Activated,#refdate ,#count ,#Name,#Area,#Region,#Code,#Name ,#Joined ,GETDATE(),#Center,#Adomstatus)
end
Insert into TSMST1003 (Device ,CreatedDate ,Subscriber,StartDate) values
(#Modem,GETDATE(),#Code,#Activated)
SET #i = #i + 1
Update TSMST1005 Set Status = 0 where Jobcode = #jobCode1
Select '3' as res
END
END
Drop table #tblSMS
end
It is quite hard to give you 100% working procedure. However your problem is that you are inserting records row by row. Instead of doing that you need to insert records in BULK. That would work A LOT faster. There is rewritten procedure. You still need to rewrite 2 inserts by the same logic. Of course there could be bugs as it is fully untested. Anyway, here we are:
USE [SMS]
GO
/****** Object: StoredProcedure [dbo].[SP_ProcessFile] Script Date: 01/30/2015 12:56:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_ProcessFile] #JobCode VARCHAR(25)
WITH RECOMPILE
AS
BEGIN
SET NOCOUNT ON
SET #Status = ( SELECT TOP 1 status
FROM TSMST1005
WHERE Jobcode = #jobCode
)
IF ( #Status = 0 )
BEGIN
SELECT '1' AS res
END
ELSE
BEGIN
CREATE TABLE #tblSMS
(
pID INT IDENTITY(1, 1) ,
[Reference] [NVARCHAR](50) NULL ,
[Date] [VARCHAR](50) NOT NULL ,
[Code] [NVARCHAR](50) NULL ,
[Subname] [VARCHAR](500) NULL ,
[Address1] [NVARCHAR](3000) NULL ,
[Address2] [NVARCHAR](500) NULL ,
[Address3] [NVARCHAR](500) NULL ,
[Joined] [VARCHAR](50) NULL ,
[Joinmonth] [VARCHAR](50) NULL ,
[Activated] [VARCHAR](50) NULL ,
[ActivMonth] [NVARCHAR](50) NULL ,
[Center] [VARCHAR](50) NULL ,
[Region] [VARCHAR](50) NULL ,
[Area] [VARCHAR](50) NULL ,
[Modem] [VARCHAR](50) NULL ,
[Adomstatus] [VARCHAR](50) NULL ,
[RefDate] [VARCHAR](50) NOT NULL
)
INSERT INTO #tblSMS
SELECT Reference ,
[Date] ,
Code ,
Subname ,
Address1 ,
Address2 ,
Address3 ,
Joined ,
Joinmonth ,
Activated ,
ActivMonth ,
Center ,
Region ,
Area ,
Modem ,
Adomstatus ,
RefDate
FROM TSMST1005
WHERE jobcode = #jobCode1
AND Status = 1
WHILE #i <= ( SELECT COUNT(*)
FROM #tblSMS
)
BEGIN
DECLARE #minPK INT;
SELECT #minPK = MAX(AddressID ) FROM TCMST5001; -- I believe that it is identity column. If not change it to the proper one
INSERT INTO TCMST5001
( CompanyCode ,
Address1 ,
Address2 ,
Address3 ,
CreatedDate ,
Status
)
SELECT '001', Address1, Address2, Address3, GETDATE(), 1 FROM #tblSMS;
SET #AddCode = 'ADD' + CAST(#count AS VARCHAR(10))
UPDATE TCMST5001
SET Code = 'ADD' + CAST(AddressID AS VARCHAR(10))
WHERE AddressID > #minPK ;
INSERT INTO TCCOM0005
SELECT ee.cnt, t.center, t.Center, 1
FROM #tblSMS t
CROSS APPLY ( SELECT COUNT(*) AS cnt FROM TCCOM0005 e WHERE e.EnterpriseUnitCode = t.Center) ee
WHERE ee.cnt > 0
-- THE SAME LOGIC MUST BE DONE WITH THESE 2 INSERTS
SET #SubCount = ( SELECT COUNT(*)
FROM TSMST1001
WHERE Subscriber = #Code
)
IF ( #SubCount = 0 )
BEGIN
INSERT INTO TSMST1001
( ActivationDate ,
refdate ,
Address ,
AlternateName ,
Area ,
Region ,
Subscriber ,
Name ,
date ,
CreatedDate ,
EnterpriseUnit ,
Status
)
VALUES ( #Activated ,
#refdate ,
#count ,
#Name ,
#Area ,
#Region ,
#Code ,
#Name ,
#Joined ,
GETDATE() ,
#Center ,
#Adomstatus
)
END
INSERT INTO TSMST1003
( Device ,
CreatedDate ,
Subscriber ,
StartDate
)
VALUES ( #Modem ,
GETDATE() ,
#Code ,
#Activated
)
UPDATE t
FROM TSMST1005 t
SET Status = 0
JOIN #tblSMS tmp
ON tmp.jobCode1 = t.Jobcode
SELECT '3' AS res
END
END
DROP TABLE #tblSMS
END
some tips will be. Avoid doing this in every step.
SELECT COUNT(*) FROM #tblSMS
instead assign the count value to a local variable, and check the i against the same.
Also you are selecting values from these tables ( TCCOM0005, TSMST1001, TSMST1003 ) frequently , It will be good to check if these tables have proper indexes.

SQL Lookup List

I have been asked to create a table in SQL and am not sure about one of the table fields.
I have been asked to create a table with the following information:
First Name
Last Name
Three Address Lines
Mobile Phone
Home Phone
Date Modified
Person Category
Comment
This is the description for the Person Category:
Person Category:
Simple Lookup List contained in the database, (ID and Description), can pre-populate this data from the script, no need to edit and update from the VB application. Category descriptions are: Client, Worker, Assessor, and Unknown.
Can someone please explain to me what is needed here?
UPDATE
Here is my current script for the Person table:
CREATE TABLE db_person.Person
(
PersonID NUMBER NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
AddressLine1 varchar(50),
AddressLine2 varchar(50),
AddressLine3 varchar(50),
MobilePhone varchar(20),
HomePhone varchar(20),
DateModified DATE,
PersonCategory varchar(50),
Comment varchar(8000),
PRIMARY KEY (PersonID)
)
Can I have some help to create the Lookup List for the PersonCategory and to link it into the Person Table?
It means that you need to create another table for the Person Category. Thus a one-to-many relationship between the 2 tables.
Moreover because the description of a person is limited to either Client, Worker, Assessor or Unknown, you should be handle this in your script creation by using the keyword "CHECK".
i.e.
CHECK (Description in ("Client", "Worker", "Assessor", "Unknown"))
First of all you must create the Categories lookup table. You could create it with a script like this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [Categories](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[Description] [varchar](50) NOT NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
ALTER TABLE [Categories] ADD CONSTRAINT [DF_Categories_Id] DEFAULT (newid()) FOR [Id]
GO
and populate it with the 4 descriptions:
INSERT INTO Categories ([Description])
VALUES ('Client')
INSERT INTO Categories ([Description])
VALUES ('Worker')
INSERT INTO Categories ([Description])
VALUES ('Assessor')
INSERT INTO Categories ([Description])
VALUES ('Unknown')
After that, you must create the Persons table, with the constraint with the Categories table:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [Persons](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[IdCategory] [uniqueidentifier] NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[FirstAddress] [varchar](250) NULL,
[SecondAddress] [varchar](250) NULL,
[ThirdAddress] [varchar](250) NULL,
[HomePhone] [varchar](50) NULL,
[MobilePhone] [varchar](50) NULL,
[Modified] [datetime] NOT NULL,
[Comment] [varchar](500) NULL,
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
ALTER TABLE [Persons] ADD CONSTRAINT [DF_Persons_Id] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [Persons] WITH CHECK ADD CONSTRAINT [FK_Persons_Categories] FOREIGN KEY([IdCategory])
REFERENCES [Categories] ([Id])
GO
ALTER TABLE [Persons] CHECK CONSTRAINT [FK_Persons_Categories]
GO
Now you have the two tables :)
These scripts run on Microsoft SQL Server.
PS: Best practice is to do another table for multiple addresses and multiple phones ;)
To retrieve data with the SELECT, you could do:
SELECT FirstName, LastName, Description AS Category
FROM Persons INNER JOIN
Categories ON Categories.Id = Persons.IdCategory
If you're using NUMBER for the IDs, change the IdCategory datatype in NUMBER. After that, the storedprocedure could be like:
CREATE PROCEDURE [AddPerson]
(
#Category varchar(50),
#FirstName varchar(50),
#LastName varchar(50),
#FirstAddress varchar(250) = NULL,
#SecondAddress varchar(250) = NULL,
#ThirdAddress varchar(250) = NULL,
#HomePhone varchar(50) = NULL,
#MobilePhone varchar(50) = NULL,
#Comment varchar(500) = NULL
)
AS BEGIN
Declare #idCategory NUMBER
Set #idCategory = SELECT Id FROM Categories WHERE Description = Category
INSERT INTO Persons ([IdCategory], [FirstName], [LastName],
[FirstAddress],
[SecondAddress],
[ThirdAddress],
[HomePhone],
[MobilePhone],
[Modified],
[Comment])
VALUES (#idCategory,
#FirstName,
#LastName,
#FirstAddress,
#SecondAddress,
#ThirdAddress,
#HomePhone,
GETDATE(),
#MobilePhone,
#Comment)
END
GO
I'm sorry if this codes contains some errors but I wrote it with notepad :)