Question : SQL  : Validation Records Before Insert

Hi Experts

I have a Stored Proc that validates data before it inserts.
The Validation only works at the first part (Serial Number Val) and stop if the Serial Number exist. which is fine
But when The Serial Number does not exist it should move to the second part and it should fail and stop the String
hole insert. And move to the Second Insert String if there is
Currentl it does not do not.

I am doing a Validation on
if exists(select * from asset where assetserial=@varSerialNumber)
if EXISTS(Select distinct Cost_center from HRFOCUS.dbo.zEmployee_ITReport Where Cost_center=@varCostCode)
if EXISTS(Select distinct Dept_Code from HRFOCUS.dbo.zEmployee_ITReport Where Dept_Code=@varBranchCode)
if EXISTS(select distinct Emp_No from HRFOCUS.dbo.zEmployee_ITReport Where Emp_No=@varEmp_No)
if exists(select TypeID from AssetType Where Typename = @varType)
if exists(select MakeID from AssetMake where makename = @varMake)
if exists(select MakeID from MakeModel where Modelname = @varModel)
if exists(select Distinct(makes.MakeID), makes.Makename from AssetMake makes Left Join MakeModel models
       on makes.MakeID = models.MakeID Where models.TypeID = @varIDAssettype and makes.Makename = @varMake)
            
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
--Create Procedure [dbo].[BulkUploadPro]
--As

Declare @varID int
Declare @varSerialNumber varchar(100)
Declare @varType varchar(100)
Declare @varMake varchar(100)
Declare @varModel varchar(100)
Declare @varStatus varchar(100)
Declare @varName varchar(100)
Declare @varSurname varchar(100)
Declare @varEmp_No varchar(100)
Declare @varCostCode varchar(100)
Declare @varEmailAddress varchar(100)
Declare @varBranchCode varchar(100)
Declare @varOfficeTel varchar(100)
Declare @varMobileNo varchar(100)
Declare @varUsername varchar(100)
Declare @varManager varchar(100)
Declare @varRAC varchar(100)
Declare @ErrorMessage varchar(350)
Declare @StatusID int
Declare @MaxID int
Declare @USERID int

set nocount on


declare temp_cursor cursor for 
select ID,SerialNumber,AssetType,AssetMake,AssetModel,Status,[Name],
	   Surname,Emp_No,CostCode,EmailAddress,BranchCode,OfficeTel,MobileNo,
	   Username,Manager,RAC
from BulkUpload

open temp_cursor

fetch next from temp_cursor
into @varID,@varSerialNumber,@varType,@varMake,@varModel,@varStatus,
	 @varName,@varSurname,@varEmp_No,@varCostCode,@varEmailAddress,
	 @varBranchCode,@varOfficeTel,@varMobileNo,@varUsername,@varManager,@varRAC

while @@fetch_status = 0
BEGIN
	set @ErrorMessage=''
	--Validation on Records Before Insert
	

	if exists(select * from asset where assetserial=@varSerialNumber)
	begin
			Set @ErrorMessage = 'Serial Number: ' + ' ' + @varSerialNumber + ' ' + ' Already Exist,' + ' ' +
						  'Asset Type:'	   + ' ' + @varType + ', ' +
						  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
						  'Asset Model:'    + ' ' + @varModel + ', ' +
										   + ' ' + 'Please Check Upload File'
					insert into BulkErrorLog (InvalidBulkRecord,Datetime)
					Values (@ErrorMessage,getdate())
	select 'debug: serial'
	end

	if @ErrorMessage=''
	begin
		if EXISTS(Select distinct Cost_center from HRFOCUS.dbo.zEmployee_ITReport Where Cost_center=@varCostCode)
		begin
				Set @ErrorMessage = 'Cost Code: ' + ' ' + @varCostCode + ' ' + ' Does not Exist in HR,' + ' ' +
							  'Asset Type:'	   + ' ' + @varType + ', ' +
							  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
							  'Asset Model:'    + ' ' + @varModel + ', ' +
											   + ' ' + 'Please Check Upload File'
						insert into BulkErrorLog (InvalidBulkRecord,Datetime)
						Values (@ErrorMessage,getdate())

	select 'debug: cost code'
		end
	end
	
	if @ErrorMessage=''
	begin
		if EXISTS(Select distinct Dept_Code from HRFOCUS.dbo.zEmployee_ITReport Where Dept_Code=@varBranchCode)
		begin
				Set @ErrorMessage = 'Branch Code: ' + ' ' + @varBranchCode + ' ' + ' Does not Exist in HR,' + ' ' +
							  'Asset Type:'	   + ' ' + @varType + ', ' +
							  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
							  'Asset Model:'    + ' ' + @varModel + ', ' +
											   + ' ' + 'Please Check Upload File'
						insert into BulkErrorLog (InvalidBulkRecord,Datetime)
						Values (@ErrorMessage,getdate())
	select 'debug: branch code'
		end
	end
	if @ErrorMessage=''
	begin
		if EXISTS(select distinct Emp_No from HRFOCUS.dbo.zEmployee_ITReport Where Emp_No=@varEmp_No)
		begin
				Set @ErrorMessage = 'Employee Number: ' + ' ' + @varEmp_No + ' ' + ' Does not Exist in HR,' + ' ' +
							  'Asset Type:'	   + ' ' + @varType + ', ' +
							  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
							  'Asset Model:'    + ' ' + @varModel + ', ' +
											   + ' ' + 'Please Check Upload File'
						insert into BulkErrorLog (InvalidBulkRecord,Datetime)
						Values (@ErrorMessage,getdate())
	select 'debug: Employee number'
		end
	end
	--Check if the Type, Make and Models Exist then assign to a variable
	if @ErrorMessage=''
	begin
		Declare @varIDAssettype int
		if exists(select TypeID from AssetType Where Typename = @varType)
		begin
			
				SET @varIDAssettype = (select TypeID from AssetType Where Typename=@varType)
		end
		ELSE
		begin
				Set @ErrorMessage = 'The Asset Type Does not Exist' + ' ' +
							  'Asset Type:'	   + ' ' + @varType + ', ' +
							  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
							  'Asset Model:'    + ' ' + @varModel + ', ' +
											   + ' ' + 'Please Check Upload File'
						insert into BulkErrorLog (InvalidBulkRecord,Datetime)
						Values (@ErrorMessage,getdate())
	select 'debug: Type Make and Model Link'
		end
	end
	if @ErrorMessage=''
	begin
		Declare @varIDassetmakename int
		if exists(select MakeID from AssetMake where makename = @varMake)
		begin
			SET @varIDassetmakename = (select MakeID from AssetMake where makename=@varMake)
		end
		ELSE
		begin
				Set @ErrorMessage = 'The Asset Make Does not Exist' + ' ' +
					  'Asset Type:'	   + ' ' + @varType + ', ' +
					  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
					  'Asset Model:'    + ' ' + @varModel + ', ' +
									   + ' ' + 'Please Check Upload File'
						insert into BulkErrorLog (InvalidBulkRecord,Datetime)
						Values (@ErrorMessage,getdate())

		select 'debug: Type Make and Model Link2'
		end
	end
	if @ErrorMessage=''
	begin
		Declare @varIDassetmodelname int
		if exists(select MakeID from MakeModel where Modelname = @varModel)
		begin
			SET @varIDassetmodelname = (select ModelId from MakeModel where Modelname=@varModel)
		end
		ELSE
		begin
						Set @ErrorMessage = 'The Asset MakeModel Does not Exist' + ' ' +
							  'Asset Type:'	   + ' ' + @varType + ', ' +
							  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
							  'Asset Model:'   + ' ' + @varModel + ', ' +
											   + ' ' + 'Please Check Upload File'
						insert into BulkErrorLog (InvalidBulkRecord,Datetime)
						Values (@ErrorMessage,getdate())
	select 'debug: Type Make and Model Link3'
		
		end
	end
	if @ErrorMessage=''
	begin
		if exists(select Distinct(makes.MakeID), makes.Makename from AssetMake makes Left Join MakeModel models
											 on makes.MakeID = models.MakeID Where models.TypeID = @varIDAssettype and makes.Makename = @varMake)
		begin
				select models.ModelId, models.ModelName from MakeModel models
				Where models.TypeID = @varIDAssettype and models.MakeID = @varIDassetmodelname and models.ModelName = @varModel
		end
		ELSE
		begin
						Set @ErrorMessage = 'The Asset MakeModel Does not Exist' + ' ' +
							  'Asset Type:'	   + ' ' + @varType + ', ' +
							  'Asset Make:'	   + ' ' + @varMake	+ ', ' +	 
							  'Asset Model:'   + ' ' + @varModel + ', ' +
											   + ' ' + 'Please Check Upload File'
						insert into BulkErrorLog (InvalidBulkRecord,Datetime)
						Values (@ErrorMessage,getdate())
	select 'debug: Type Make and Model Link4'
		end
	end
	if @ErrorMessage=''
	begin
		-- Insert User Details into table
		INSERT INTO UserDetails ([Name],Surname,OfficeNumber,CellNumber,Emailaddress,employeeNumber,CostCodes,BranchCostCode,RACName,ManagerName,username)
		Values(@varName,@varSurname,@varOfficeTel,@varMobileNo,@varEmailAddress,@varEmp_No,@varCostCode,@varBranchCode,@varRAC,@varManager,@varUsername)
		
		
		-- Get Status ID
		SET @StatusID = (select StatusID From dbo.Status Where Statusname=@varStatus)
		
		-- Insert Type,Makeand Model ID into Main Table
		INSERT INTO Asset (TypeID, MakeID, ModelID, statusid , AssetSerial, DateModified, DateCreated)
		Values(@varIDAssettype,@varIDassetmakename,@varIDassetmodelname,@StatusID,@varSerialNumber,getdate(),getdate() )

		SET @MaxID = (select max(assetid) from asset)
		
		SET @USERID = (Select ID from userdetails Where costcodes = @varCostCode)

		INSERT INTO Asset_Branch_Link (AssetID,UserDetailsid)
		Values (@MaxID,@USERID)
	end

	fetch next from temp_cursor
	into @varID,@varSerialNumber,@varType,@varMake,@varModel,@varStatus,
	 @varName,@varSurname,@varEmp_No,@varCostCode,@varEmailAddress,
	 @varBranchCode,@varOfficeTel,@varMobileNo,@varUsername,@varManager,@varRAC


END
CLOSE temp_cursor
deallocate temp_cursor
--Truncate Table BulkUpload

Answer : SQL  : Validation Records Before Insert

your code looks fine.  if you have ANSI_NULLS off, try setting it on at the top of your procedure.  this will affect handling of null values and prevent matches if a null is encountered.

set ansi_nulls on
Random Solutions  
 
programming4us programming4us