Link to home
Start Free TrialLog in
Avatar of Sheritlw
SheritlwFlag for United States of America

asked on

SQL check for value if exists set value to variable

I'm not sure how I would do this, but in a stored procedure I would like to perform a check on a value and if the value exists set the value to a variable.

Kind of like the following...

if exists(select prodimpid from producttypeslu where prodimpid = @impProdCatID)
 select @BrandID = BrandID
 
else
 Do an import and then
Select @BrandID = Scope_Identity()

I hope this makes since.

Thanks

if exists(select prodimpid from brandslu where where prodimpid = @impprodlineid)
		select @BrandID = brandid from brandslu where prodimpid = @impprodlineid
	else

		Insert into BrandsLU(UserID, BrandName)
		 select @UserID, ProductLine from ImportProductLine where 
		impProdLineID = @impProdLineID
		
		Select @BrandID = Scope_Identity()

Open in new window

Avatar of Jim Horn
Jim Horn
Flag of United States of America image

One way...

Declare @count int

SELECT @count = COUNT(prodimpid) from producttypeslu where prodimpid = @impProdCatID

IF @COUNT > 0
   begin
   @BrandID = BrandID
   end
ELSE
   begin
   -- Do an import and then
   @BrandID = Scope_Identity()
   end
Avatar of Sheritlw

ASKER


But wouldn't I have to do another select in order to get the brandid?
I was hoping I could set the variable without having to do another select.

SELECT @count = COUNT(prodimpid) from producttypeslu where prodimpid = @impProdCatID

IF @COUNT > 0
   begin
   Select @BrandID = BrandID from producttypeslu where prodimpid = @impProdCatID
   end
ELSE
   begin
   -- Do an import and then
   @BrandID = Scope_Identity()
   end
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you