user defined function return statment in Scalar-Valued - sql

I have a table with the following columns:
CREATE TABLE [dbo].[TblPerson](
[Id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](30) NULL,
[Family] [nvarchar](40) NULL,
[address] [nvarchar](200) NULL,
[MeliCode] [char](10) NOT NULL,
[IsActive] [bit] NULL CONSTRAINT [DF_TblPerson_IsActive] DEFAULT ((0)),
[Count] [int] NULL CONSTRAINT [DF_TblPerson_Count] DEFAULT ((0)),
CONSTRAINT [PK_TblPerson] PRIMARY KEY CLUSTERED
(
Now i want to write a UDF which gets the Melicode as input parameter and if the IsActive = 1 then the UDF returns the value of count, else the UDF must return 0.
i write the following function as
CREATE FUNCTION ReturnMelli ( #Melicode CHAR(10) )
RETURNS INT
AS
BEGIN
DECLARE #RESULT BIT
SET #RESULT = ( SELECT Isactive
FROM TblPerson
WHERE MeliCode = #Melicode
)
DECLARE #R INT
SET #R = ( SELECT [Count]
FROM TblPerson
WHERE MeliCode = #Melicode
)
IF ( #RESULT = 1 )
BEGIN
RETURN #R
END
ELSE
BEGIN
RETURN 0
END
END
Then, there is problem with running the UDF as
Msg 455, Level 16, State 2, Procedure ReturnMelli, Line 17
The last statement included within a function must be a return statement.
i don't get the reason of failure. Even i put the return value at the end, it doesn't work

Replace this
IF ( #RESULT = 1 )
BEGIN
RETURN #R
END
ELSE
BEGIN
RETURN 0
END
with this
RETURN IIF(#RESULT = 1, #R, 0)

This link will Help you
Try like this ..
CREATE FUNCTION ReturnMelli ( #Melicode CHAR(10) )
RETURNS INT
AS
BEGIN
DECLARE #RESULT BIT
SET #RESULT = ( SELECT Isactive
FROM TblPerson
WHERE MeliCode = #Melicode
)
DECLARE #R INT
SET #R = ( SELECT [Count]
FROM TblPerson
WHERE MeliCode = #Melicode
)
IF ( #RESULT <> 1 )
BEGIN
SET #R =0
END
RETURN #R
END

Related

Create a function to check if the quantity inserted is greater than the quantity in stock

CREATE TABLE [dbo].[OrderDetails](
[OrderID] [int] NOT NULL,
[ProductID] [int] NOT NULL,
[UnitPrice] [int] NOT NULL,
[Quantity] [tinyint] NULL,
[Discount] [int] NULL,
[IsActive] [bit] NULL,
[IsDeleted] [bit] NULL
) ON [PRIMARY]
I want to create a function which will check if the quantity inserted is greater then the quantity in stock then prevents user from execution.
GO
SET QUOTED_IDENTIFIER ON;
GO
CREATE TRIGGER dbo.PreventQtyInsert ON dbo.OrderDetails
AFTER INSERT
AS
BEGIN
DECLARE #returnQuantity INT ,
#Quantity INT ,
#ProductID INT ,
#GetQuantityFromProductsTable INT;
SELECT #returnQuantity = dbo.ReturnUnitsInStock ( #Quantity , #ProductID ) ;
IF #returnQuantity = 0
BEGIN
RAISERROR ( 'This vendor''s credit rating is too low to accept new purchase orders.' , 16 , 1 ) ;
ROLLBACK TRANSACTION;
RETURN;
END;
ELSE
BEGIN
SET #GetQuantityFromProductsTable = ( SELECT UnitsInStock FROM Products WHERE ProductID = #ProductID ) ;
UPDATE OrderDetails SET Quantity = #GetQuantityFromProductsTable - #Quantity
WHERE ProductID = #ProductID;
END;
END;
GO

Run large compare in Batches

I need to compare large tables with millions of rows and insert the differences into a log table.
The problem is that stored proc grows (LDF), and the database disk space is limited.
I know that commit will write the LDF to the MDF.
How can I perform the following compare in batches, and commit every hundred thousand rows?
BEGIN TRY
BEGIN TRANSACTION;
INSERT INTO dbo.CustomerLog
( OnlineStore ,
PhoneNumber ,
ChangeType
)
SELECT 'Online Store a' ,
AreaCode + PhoneNumber ,
'Added'
FROM dbo.StoreAList a
WHERE NOT EXISTS ( SELECT 1
FROM dbo.StoreAListCompare b
WHERE ( b.AreaCode + b.PhoneNumber ) = ( a.AreaCode
+ a.PhoneNumber ) );
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH;
CREATE TABLE [dbo].[StoreAList](
[ListID] [bigint] IDENTITY(1,1) NOT NULL,
[AreaCode] [char](3) NOT NULL,
[PhoneNumber] [char](7) NOT NULL,
[RecordDate] [datetime] NULL CONSTRAINT [DF_StoreAList_RecordDate] DEFAULT (getdate())
) ON [PRIMARY]
CREATE TABLE [dnc].[StoreAListCompare](
[ListID] [BIGINT] IDENTITY(1,1) NOT NULL,
[AreaCode] [CHAR](3) NOT NULL,
[PhoneNumber] [CHAR](7) NOT NULL,
[RecordDate] [DATETIME] NULL DEFAULT (GETDATE())
) ON [PRIMARY]
You can use the ##rowcount system variable and do the insert in batches until ##rowcount hits 0.
Note the added AND NOT EXISTS in dbo.CustomerLog...
Ex:
DECLARE #BATCHSIZE INT=100000
WHILE #BATCHSIZE>0
BEGIN
BEGIN TRY
BEGIN TRANSACTION;
INSERT INTO dbo.CustomerLog
( OnlineStore ,
PhoneNumber ,
ChangeType
)
SELECT TOP(#BATCHSIZE)
'Online Store a' ,
AreaCode + PhoneNumber ,
'Added'
FROM dbo.StoreAList a
WHERE NOT EXISTS ( SELECT 1
FROM dbo.StoreAListCompare b
WHERE ( b.AreaCode + b.PhoneNumber ) = ( a.AreaCode + a.PhoneNumber ) )
AND NOT EXISTS (SELECT 1
FROM dbo.CustomerLog CL
WHERE 'Online Store a'=CL.OnlineStore
AND AreaCode + PhoneNumber=CL.PhoneNumber
AND 'Added'=CL.ChangeType);
SET #BATCHSIZE=##ROWCOUNT
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SET #BATCHSIZE=0
END CATCH;
END

Table-Valued Function using IF statement in SQL Server

I have a Student table consists of following parameters
[ID] [nvarchar](50) NOT NULL,
[Firsname] [nvarchar](50) NOT NULL,
[Lastname] [nvarchar](50) NOT NULL,
[Melicode] [nchar](10) NOT NULL,
[City] [nvarchar](50) NOT NULL,
[Province] [nvarchar](50) NOT NULL,
[Active] [int] NULL
i want to write a Table-Valued Function named Show which has one parameter as number. the function will act as following
if #number = 1 , returns all columns from Student table
if #number = 2 , returns only City from Student
if #number = 3 , returns only Province from Student
i wrote the following T-SQL, but it only works for (if (#number = 1)). When the user enter #number as 2 or 3, the function does not work. Thank You
Create function Show(#number int)
RETURNS #result TABLE
(
[ID] [nvarchar](50) NOT NULL,
[Firsname] [nvarchar](50) NOT NULL,
[Lastname] [nvarchar](50) NOT NULL,
[Melicode] [nchar](10) NOT NULL,
[City] [nvarchar](50) NOT NULL,
[Province] [nvarchar](50) NOT NULL,
[Active] [int] NULL
)
AS
BEGIN
IF (#number = 1)
INSERT INTO #result SELECT * from Student
IF (#number = 2)
INSERT INTO #result (City) values ((SELECT City from Student))
IF (#number = 3)
INSERT INTO #result (Province) values ((SELECT Province from Student))
RETURN -- #Players (variable only required for Scalar functions)
END
go
select *from dbo.show(1)
This is not going to work:
INSERT INTO #result (City)
VALUES ((SELECT City from Student))
Either you have all the values as scalar SQL variables, or literals - then you can use
INSERT INTO #result (City)
VALUES ('New York')
INSERT INTO #result (City)
VALUES (#ChosenCity)
or you have a SELECT statement to fill the values - then you need this syntax:
INSERT INTO #result (City)
SELECT City
FROM Student
without the VALUES keyword. And as #GiorgiNakeuri correctly states - this will then fail because all your columns require a value (have the NOT NULL attribute), so this insert cannot succeed - you need to provide all NOT NULL values (or define a default value for each column)
CREATE FUNCTION dbo.Show
(
#number INT
)
RETURNS #result TABLE
(
ID NVARCHAR(50),
Firsname NVARCHAR(50),
Lastname NVARCHAR(50),
Melicode NCHAR(10),
City NVARCHAR(50),
Province NVARCHAR(50),
Active INT
)
AS
BEGIN
IF (#number = 1)
INSERT INTO #result
SELECT * FROM dbo.Student
IF (#number = 2)
INSERT INTO #result (City)
SELECT City FROM dbo.Student
IF (#number = 3)
INSERT INTO #result (Province)
SELECT Province FROM dbo.Student
RETURN
END
GO
SELECT * FROM dbo.Show(2)
the table returned is dictated by how the result table was declared. the query below works (in a sense) but the results include all the columns with NULLs for those columns not targeted by the #number parameter:
CREATE TABLE dbo.z_Show (str1 VARCHAR(10), str2 VARCHAR(10), str3 VARCHAR(10))
INSERT z_show
SELECT 1, 1, 1 UNION ALL
SELECT 2, 2, 2 UNION ALL
SELECT 3, 3, 3
CREATE FUNCTION dbo.Show(#number int)
RETURNS #result TABLE
(
--[ID] [nvarchar](50) NOT NULL,
--[Firsname] [nvarchar](50) NOT NULL,
--[Lastname] [nvarchar](50) NOT NULL,
--[Melicode] [nchar](10) NOT NULL,
--[City] [nvarchar](50) NOT NULL,
--[Province] [nvarchar](50) NOT NULL,
--[Active] [int] NULL
str1 VARCHAR(10), str2 VARCHAR(10), str3 VARCHAR(10)
)
AS
BEGIN
--for debugging|start
--DECLARE #number INT = 3
--DECLARE #result TABLE (str1 VARCHAR(10), str2 VARCHAR(10), str3 VARCHAR(10))
--for debugging|end
IF (#number = 1)
BEGIN
--PRINT ('IF (#number = 1)')
INSERT INTO #result SELECT * from dbo.z_Show
END
IF (#number = 2)
BEGIN
--PRINT ('IF (#number = 2)')
INSERT INTO #result (str2) SELECT str2 from dbo.z_Show
END
IF (#number = 3)
BEGIN
--PRINT ('IF (#number = 3)')
INSERT INTO #result (str3) SELECT str3 from dbo.z_Show
END
RETURN -- #Players (variable only required for Scalar functions)
END
SELECT 'number 1 was passed', *
FROM dbo.show(1)
SELECT 'number 2 was passed', *
FROM dbo.show(2)
SELECT 'number 3 was passed', *
FROM dbo.show(3)
You mentioned #result has all NOT NULL columns. If you want to insert only city into that #result, it will take remaining columns as Null so that's why an error happened. You don't mention that #result columns are NOT NULL columns and one more is. Remove VALUES keyword from the INSERT statement because it is inserting with a Select statement
The insert statements for cases 2 and 3 are incorrect. No need for VALUES keyword when inserting values coming from a select statement.

Sql stored procedure taking lot of time for execution

I am facing a big issue, the below stored procedure taking lot of time for execution. Please help me to find issues with following stored procedure .
We need to insert bulk subscriber list from excel in to database. But only 60 subscribers are getting inserted in to db in one minute.
Please help me to resolve the issue.
USE [SMS]
GO
/****** Object: StoredProcedure [dbo].[SP_ProcessFile] Script Date: 01/30/2015 12:56:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_ProcessFile]
#JobCode varchar(25)
WITH RECOMPILE
AS
declare
#jobCode1 Varchar(50),
#count int,#Code varchar(50),#Name [varchar](50),#Date Datetime,#Status int,
#i int,#EUCount int,#SubCount int,
#Add1 nvarchar(3000) ,
#Add2 nvarchar(500) ,
#Add3 nvarchar(500)
,#refdate [varchar](50) ,
#reference [varchar](50) ,
#Joined [varchar](50),
#Joinmonth [nvarchar](50),
#Activated [varchar](50),
#ActivMonth [nvarchar](50),
#Center [varchar](50) ,
#Region [varchar](50) ,
#Area [varchar](50) ,
#Modem [varchar](50) ,
#Adomstatus [varchar](50)
, #AddCode [varchar](50)
set #i = 1
Set #jobCode1 =#JobCode
BEGIN
SET NOCOUNT ON
Set #Status = (Select Distinct(status) from TSMST1005 where Jobcode = #jobCode1)
if (#Status = 0)
begin
Select '1' as res
end
else
begin
CREATE TABLE #tblSMS(pID int identity(1,1),
[Reference] [nvarchar](50) NULL,
[Date] [varchar](50) NOT NULL,
[Code] [nvarchar](50) NULL,
[Subname] [varchar](500) NULL,
[Address1] [nvarchar](3000) NULL,
[Address2] [nvarchar](500) NULL,
[Address3] [nvarchar](500) NULL,
[Joined] [varchar](50) NULL,
[Joinmonth] [varchar](50) NULL,
[Activated] [varchar](50) NULL,
[ActivMonth] [nvarchar](50) NULL,
[Center] [varchar](50) NULL,
[Region] [varchar](50) NULL,
[Area] [varchar](50) NULL,
[Modem] [varchar](50) NULL,
[Adomstatus] [varchar](50) NULL,
[RefDate] [varchar](50) NOT NULL)
insert into #tblSMS
SELECT Reference,[Date],
Code,Subname ,Address1 ,Address2 ,Address3 ,
Joined , Joinmonth ,Activated, ActivMonth ,
Center,Region,Area,Modem,Adomstatus ,refdate FROM TSMST1005 where jobcode = #jobCode1 and Status =1
WHILE #i <= (SELECT COUNT(*) FROM #tblSMS)
BEGIN
SELECT
#Code =Code,
#Name = Subname,
#Date =[Date],
#Add1 =Address1 ,
#Add2 =Address2 ,
#Add3= Address3,
#reference =Reference ,
#Joined = Joined,
#Joinmonth =Joinmonth,
#Activated =Activated,
#ActivMonth =ActivMonth,
#Center = Center,
#Region = Region,
#Area= Area,
#Modem = Modem ,
#Adomstatus =Adomstatus,
#refdate = RefDate
From #tblSMS where pID = #i
Insert into TCMST5001 (CompanyCode , Address1,Address2 ,Address3 ,CreatedDate ,Status) values('001',#Add1 ,#Add2,#Add3,GETDATE(),1)
Set #count = SCOPE_IDENTITY()
Set #AddCode = 'ADD' + Cast(#count As Varchar(10))
Update TCMST5001 Set Code =#AddCode Where AddressID =#count
Set #EUCount = (Select COUNT(*) from TCCOM0005 where EnterpriseUnitCode = #Center)
if (#EUCount = 0)
Begin
Insert into TCCOM0005(AddressCode,CompanyCode,EnterpriseUnitCode,EnterpriseUnitName,Status) values(#count ,'001',#Center,#Center ,1)
END
Set #SubCount = (Select COUNT(*) from TSMST1001 where Subscriber = #Code)
if (#SubCount =0)
begin
Insert into TSMST1001(ActivationDate ,refdate , Address ,AlternateName ,Area ,Region ,Subscriber,Name ,date ,CreatedDate ,EnterpriseUnit ,Status)
values(#Activated,#refdate ,#count ,#Name,#Area,#Region,#Code,#Name ,#Joined ,GETDATE(),#Center,#Adomstatus)
end
Insert into TSMST1003 (Device ,CreatedDate ,Subscriber,StartDate) values
(#Modem,GETDATE(),#Code,#Activated)
SET #i = #i + 1
Update TSMST1005 Set Status = 0 where Jobcode = #jobCode1
Select '3' as res
END
END
Drop table #tblSMS
end
It is quite hard to give you 100% working procedure. However your problem is that you are inserting records row by row. Instead of doing that you need to insert records in BULK. That would work A LOT faster. There is rewritten procedure. You still need to rewrite 2 inserts by the same logic. Of course there could be bugs as it is fully untested. Anyway, here we are:
USE [SMS]
GO
/****** Object: StoredProcedure [dbo].[SP_ProcessFile] Script Date: 01/30/2015 12:56:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_ProcessFile] #JobCode VARCHAR(25)
WITH RECOMPILE
AS
BEGIN
SET NOCOUNT ON
SET #Status = ( SELECT TOP 1 status
FROM TSMST1005
WHERE Jobcode = #jobCode
)
IF ( #Status = 0 )
BEGIN
SELECT '1' AS res
END
ELSE
BEGIN
CREATE TABLE #tblSMS
(
pID INT IDENTITY(1, 1) ,
[Reference] [NVARCHAR](50) NULL ,
[Date] [VARCHAR](50) NOT NULL ,
[Code] [NVARCHAR](50) NULL ,
[Subname] [VARCHAR](500) NULL ,
[Address1] [NVARCHAR](3000) NULL ,
[Address2] [NVARCHAR](500) NULL ,
[Address3] [NVARCHAR](500) NULL ,
[Joined] [VARCHAR](50) NULL ,
[Joinmonth] [VARCHAR](50) NULL ,
[Activated] [VARCHAR](50) NULL ,
[ActivMonth] [NVARCHAR](50) NULL ,
[Center] [VARCHAR](50) NULL ,
[Region] [VARCHAR](50) NULL ,
[Area] [VARCHAR](50) NULL ,
[Modem] [VARCHAR](50) NULL ,
[Adomstatus] [VARCHAR](50) NULL ,
[RefDate] [VARCHAR](50) NOT NULL
)
INSERT INTO #tblSMS
SELECT Reference ,
[Date] ,
Code ,
Subname ,
Address1 ,
Address2 ,
Address3 ,
Joined ,
Joinmonth ,
Activated ,
ActivMonth ,
Center ,
Region ,
Area ,
Modem ,
Adomstatus ,
RefDate
FROM TSMST1005
WHERE jobcode = #jobCode1
AND Status = 1
WHILE #i <= ( SELECT COUNT(*)
FROM #tblSMS
)
BEGIN
DECLARE #minPK INT;
SELECT #minPK = MAX(AddressID ) FROM TCMST5001; -- I believe that it is identity column. If not change it to the proper one
INSERT INTO TCMST5001
( CompanyCode ,
Address1 ,
Address2 ,
Address3 ,
CreatedDate ,
Status
)
SELECT '001', Address1, Address2, Address3, GETDATE(), 1 FROM #tblSMS;
SET #AddCode = 'ADD' + CAST(#count AS VARCHAR(10))
UPDATE TCMST5001
SET Code = 'ADD' + CAST(AddressID AS VARCHAR(10))
WHERE AddressID > #minPK ;
INSERT INTO TCCOM0005
SELECT ee.cnt, t.center, t.Center, 1
FROM #tblSMS t
CROSS APPLY ( SELECT COUNT(*) AS cnt FROM TCCOM0005 e WHERE e.EnterpriseUnitCode = t.Center) ee
WHERE ee.cnt > 0
-- THE SAME LOGIC MUST BE DONE WITH THESE 2 INSERTS
SET #SubCount = ( SELECT COUNT(*)
FROM TSMST1001
WHERE Subscriber = #Code
)
IF ( #SubCount = 0 )
BEGIN
INSERT INTO TSMST1001
( ActivationDate ,
refdate ,
Address ,
AlternateName ,
Area ,
Region ,
Subscriber ,
Name ,
date ,
CreatedDate ,
EnterpriseUnit ,
Status
)
VALUES ( #Activated ,
#refdate ,
#count ,
#Name ,
#Area ,
#Region ,
#Code ,
#Name ,
#Joined ,
GETDATE() ,
#Center ,
#Adomstatus
)
END
INSERT INTO TSMST1003
( Device ,
CreatedDate ,
Subscriber ,
StartDate
)
VALUES ( #Modem ,
GETDATE() ,
#Code ,
#Activated
)
UPDATE t
FROM TSMST1005 t
SET Status = 0
JOIN #tblSMS tmp
ON tmp.jobCode1 = t.Jobcode
SELECT '3' AS res
END
END
DROP TABLE #tblSMS
END
some tips will be. Avoid doing this in every step.
SELECT COUNT(*) FROM #tblSMS
instead assign the count value to a local variable, and check the i against the same.
Also you are selecting values from these tables ( TCCOM0005, TSMST1001, TSMST1003 ) frequently , It will be good to check if these tables have proper indexes.

SQL problem: same column in one

hy!
I have 2 tables and in each I have a column date, I need to make a single table with the information from all 2 tables with a column date which i want to get from the 3 tables,but in the same column
i tried the following code, but didn`t work
CREATE FUNCTION dbo.GetContactInformation(#id int)
RETURNS #retActivityInformation TABLE
(
ClientID int NOT NULL,
ActivityDate datetime NULL,
Tipe nvarchar(50) NULL,
Number nvarchar(50) NULL,
Value int NULL,
Statu nvarchar(50) NULL,
PRIMARY KEY CLUSTERED (clientID ASC)
) AS
BEGIN
DECLARE
#ClientID int,
#ActivityDate datetime,
#Tip nvarchar(50),
#Number nvarchar(50),
#Value int,
#Statu nvarchar(50);
SELECT
#ClientID = ClientID,
#ActivityDate = ActivityDate,
#Number = Number,
#Value = Value,
#Statu = Statu
FROM Fa,Pay
WHERE ID = #id;
SET #ActivityDate =
CASE
WHEN EXISTS(SELECT Fa.DataEmitere FROM Fa AS e
WHERE e.ID = #id)
THEN 'Fa'
WHEN EXISTS(SELECT Pay.Data FROM Pay AS bec
WHERE bec.ID = #id)
THEN 'Pay'
END;
IF #id IS NOT NULL
BEGIN
INSERT #retActivityInformation
SELECT #clientID, #ActivityDate, #Number, #Value,#Statu;
END;
RETURN;
END;
Just prefix the field with the database name. I am going to assume the date you actually mean is ActivityDate. If you want to SELECT/INSERT using this field you will need to prefix with Fa or Pay so it would be Fa.ActivityDate or Pay.ActivityDate.
If this is not the field then we'd need more info.
Use the column by specifying the table name as below:-
CREATE FUNCTION dbo.GetContactInformation(#id int)
RETURNS #retActivityInformation TABLE
(
ClientID int NOT NULL,
ActivityDate datetime NULL,
Tipe nvarchar(50) NULL,
Number nvarchar(50) NULL,
Value int NULL,
Statu nvarchar(50) NULL,
PRIMARY KEY CLUSTERED (clientID ASC)
) AS
BEGIN
DECLARE
#ClientID int,
#ActivityDate datetime,
#Tip nvarchar(50),
#Number nvarchar(50),
#Value int,
#Statu nvarchar(50);
SELECT
#ClientID = ClientID,
#ActivityDate = Fa.ActivityDate,
#Number = Number,
#Value = Value,
#Statu = Statu
FROM Fa,Pay
WHERE ID = #id;
SET #ActivityDate =
CASE
WHEN EXISTS(SELECT Fa.DataEmitere FROM Fa AS e
WHERE e.ID = #id)
THEN 'Fa'
WHEN EXISTS(SELECT Pay.Data FROM Pay AS bec
WHERE bec.ID = #id)
THEN 'Pay'
END;
IF #id IS NOT NULL
BEGIN
INSERT #retActivityInformation
SELECT #clientID, #ActivityDate, #Number, #Value,#Statu;
END;
RETURN;
END;
See the middle part here:
CREATE FUNCTION dbo.GetContactInformation(#id int)
RETURNS #retActivityInformation TABLE
(
ClientID int NOT NULL,
ActivityDate datetime NULL,
Tipe nvarchar(50) NULL,
Number nvarchar(50) NULL,
Value int NULL,
Statu nvarchar(50) NULL,
PRIMARY KEY CLUSTERED (clientID ASC)
) AS
BEGIN
DECLARE
#ClientID int,
#ActivityDate datetime,
#Tip nvarchar(50),
#Number nvarchar(50),
#Value int,
#Statu nvarchar(50);
SELECT
#ClientID = ClientID,
#ActivityDate = ActivityDate,
#Number = Number,
#Value = Value,
#Statu = Statu
FROM Fa,Pay
WHERE ID = #id;
SET #ActivityDate = ISNULL(
(SELECT top 1 Fa.DataEmitere FROM Fa AS e WHERE e.ID = #id),
(SELECT top 1 Pay.Data FROM Pay AS bec WHERE bec.ID = #id))
IF #id IS NOT NULL
BEGIN
INSERT #retActivityInformation
SELECT #clientID, #ActivityDate, #Number, #Value,#Statu;
END;
RETURN;
END;
Essentially, instead of testing to see if the data EXISTS just to get the field name, get the data directly.