In my database i have two types of applications: with serial number and without serial number. I want to create a function in SQL Server that takes as parameters 2 nvarchars: first is the application code and the other is the serial number and returns a table type with the available versions. This function should first check if the application has serial number or not and depending on the answer, which is bit type, call the proper function. How can i put this together?
table getVersions(appCode, serial){
if(isFree(appCode)){
call getAppVersionsFree(appCode);
} else {
call getAppVersions(appCode,serial);
}
return table;
The function that checks if the application has serial number or is free:
create function dbo.isFree(
@appCode nvarchar(128))
returns bit
as
begin
declare @ret bit
select @ret = (select a.IsFree from Application a where a.Code=@appCode)
return @ret
end
Function to be called if the application is free:
create function dbo.getAppVersionsFree
(
@appCode nvarchar(128))
returns table as
return (
select v.Version from Version v
inner join Application a on a.Code = v.AppCode
inner join SerialNumber s on a.Code = s.AppCode
where a.Code = @appCode
)
Function to be called if the application is not free (with serial number):
create function dbo.getAppVersions
(
@appCode nvarchar(128),
@serialNo nvarchar(128))
returns table as
return (
select v.Version from Version v
inner join Application a on a.Code = v.AppCode
inner join SerialNumber s on a.Code = s.AppCode
where a.Code = @appCode and s.SerialNo = @serialNo
)
EDIT:
How to make this to work?
create function dbo.getVersions
(
@appCode nvarchar(128),
@serialNo nvarchar(128))
returns table
as
begin
declare @returnTable table (Version nvarchar(10))
if(dbo.isFree(@appCode) = 0) begin
set @returnTable = select dbo.getAppVersions(@appCode,@serialNo)
end
else begin
set @returnTable = select dbo.getAppVersionsFree(@appCode)
end
return @returnTable;
end