Sheritlw
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
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()
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
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