Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
4.56/5 (2 votes)
See more:
I have created a table called 'Names' in SQL with the following 2 columns

ID	AUTO ID
NAME	AS nVarchar(MAX)


I have a stored procedure to add Name in it if that name doesn't exist already as

SQL
CREATE PROCEDURE [dbo].[Ins_NameNew] 
(
	@Name_Input	varChar(Max)
)
AS 
BEGIN
	DECLARE @NameExist INT
	DECLARE @NameID INT
	DECLARE @Action NVARCHAR(5)
	
	-- Check if the name exists
	SET @NameExist =
			CASE
				WHEN EXISTS (SELECT * FROM [dBName].[dbo].[Names] WHERE Name = @Name_Input )
				THEN 1
				ELSE 0
			END
	
	-- Proceed with Insert if doesn't exist
	IF @NameExist = 0

	BEGIN
			INSERT INTO [dBName].[dbo].[Names]
			(Name)
			VALUES
				(@Name_Input);
	END
	
	-- Get the ID whether Name existed already or just added	
	SET @NameID = (SELECT ID FROM [dBName].[dbo].[Names] WHERE Name = @Name_Input)
	
	
	SET @Action =
		CASE
			WHEN @NameExist = 0
			THEN 'Added'
			ELSE 'Exist'
		END

	SELECT @NameExist, @NameID, @Action
END


This works fine when it comes to adding the new name. But each time it returns @NameExist as 1 and @Action as 'Exist'

Any suggestions where I am going wrong with the stored proc.

What I have tried:

SQL
CREATE PROCEDURE [dbo].[Ins_NameNew] 
(
	@Name_Input	varChar(Max)
)
AS 
BEGIN
	DECLARE @NameExist INT
	DECLARE @NameID INT
	DECLARE @Action NVARCHAR(5)
	
	-- Check if the name exists
	SET @NameExist =
			CASE
				WHEN EXISTS (SELECT * FROM [dBName].[dbo].[Names] WHERE Name = @Name_Input )
				THEN 1
				ELSE 0
			END
	
	-- Proceed with Insert if doesn't exist
	IF @NameExist = 0

	BEGIN
			INSERT INTO [dBName].[dbo].[Names]
			(Name)
			VALUES
				(@Name_Input);
	END
	
	-- Get the ID whether Name existed already or just added	
	SET @NameID = (SELECT ID FROM [dBName].[dbo].[Names] WHERE Name = @Name_Input)
	
	
	SET @Action =
		CASE
			WHEN @NameExist = 0
			THEN 'Added'
			ELSE 'Exist'
		END

	SELECT @NameExist, @NameID, @Action
END
Posted
Updated 17-Nov-16 12:35pm
v3
Comments
DinoOnCodeProject 7-Nov-16 11:59am    
Kindly excuse the grammatical errors like ' as I am very new to this site and slowly learning how to post questions. Hope the question doesn't sound too confusing due to this. I can see that it is replacing ' with '. Will be more careful next time.
Thanks in advance.
DinoOnCodeProject 7-Nov-16 12:21pm    
Thanks @PIEBALDconsult for correcting me. :)
Herman<T>.Instance 8-Nov-16 7:28am    

1 solution

Hello,

Seems that your ID is an Identity column, you could easily simply your code by doing something like this.

SQL
CREATE PROCEDURE [dbo].[Ins_NameNew] 
(
	@Name_Input	varChar(Max)
)
AS 
BEGIN
	DECLARE @NameExist INT = 0
	DECLARE @NameID INT

	-- Proceed with Insert if doesn't exist
	IF EXISTS (SELECT * FROM [dBName].[dbo].[Names] WHERE Name = @Name_Input)
	BEGIN
			INSERT INTO [dBName].[dbo].[Names]
			(Name)
			VALUES
				(@Name_Input);
                       SET @NameID = @@IDENTITY;
	END
	ELSE
        BEGIN
                -- Get the ID whether Name existed already or just added, )
                -- remove top 1 if you Name is unique
	       SET @NameID = (SELECT TOP 1 ID FROM [dBName].[dbo].[Names] WHERE Name = @Name_Input)
                SET @NameExist = 1;
        END

 
	SELECT @NameExist, 
                      @NameID, 
                CASE
			WHEN @NameExist = 0
			THEN 'Added'
			ELSE 'Exist'
		END as 'Action'
END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900