I need some help with an SQL stored procedure, I haven’t got alot of experience with it.
I have a table “dbo.Lookup_Country”. It contains 3 fields: ID, Name, Code.
Now I wrote a stored procedure that retrieves the country name by Code.
Select Name from Lookup_Country where Code = @Code
I need to adjust the stored procedure so that if there are 0 results it has to see if there are results by searching with ID:
Select Name from Lookup_Country where Id = @Code
So basically I need some kind of ‘IF’ structure:
if (Select Name from Lookup_Country where Code = @Code) == 0 results
Select Name from Lookup_Country where Id = @Code
Is this possible?
thx
Thx for the help everyone, I used this:
DECLARE @Name nvarchar(50)
-- Insert statements for procedure here
SET @Name = (SELECT Name from dbo.Lookup_Country WHERE Code = @CODE)
IF @Name != ''
SELECT Name from dbo.Lookup_Country WHERE Code = @CODE
ELSE
SELECT Name from dbo.Lookup_Country WHERE ID = @CODE
Put the results of the first select into a variable, then you can do a count on that result, and if that is zero then you would do the search by id.
To learn more about how to use the
ifstatement then check out this article, if you are using SQL Server, but, if you are using a different database then the logic still applies theifsyntax may differ.http://msdn.microsoft.com/en-us/library/ms182587.aspx