Temp table has only one row inserted - sql

Hi I have an SP in which i create a temporary table to store some values.
ALTER PROCEDURE [dbo].[test]
#id int,
#funds_limit money
AS
BEGIN
SET NOCOUNT ON;
DECLARE #threshold money;
CREATE TABLE #ConfigurationTemp
(id int,
name varchar(100) not null,
type varchar(100),
value varchar(100))
INSERT #ConfigurationTemp EXEC get_config #id, 'testType', null
select #threshold = value
from #ConfigurationTemp
where id=#id and name='testLimit'
print #threshold
IF (#funds_limit IS NOT NULL) AND (#threshold < #funds_limit)
BEGIN
DROP TABLE #ConfigurationTemp;
RETURN 1000;
END
select #threshold = value
from #ConfigurationTemp
where id=#id and name='testLimit1'
print #threshold
IF (#funds_limit IS NOT NULL) AND (#threshold < #funds_limit)
BEGIN
DROP TABLE #ConfigurationTemp;
RETURN 1001;
END
END
RETURN 0;
END
The temporary table have multiple rows.
eg:
1, fund_limit, testType, 10
2, fund_min_limit, testType, 20
I need to first validate the value for fund_limit (10) with the user input value (which will be an input parameter to the SP). If the validation fails, i return with an error code. If not, I go for the next check. i.e., fund_min_limit. I do the same with it and return a different error code. If no validation fails, i will return 0 which is considered to be a success.
In this case, I am getting same value for threshold always. i.e., the value of first row... 10.
How can I get the different threshold value from the temp table with respect to the name?

When you assign scalar variable with select - it may be not assigned (unchanged - may keep value from previous assignment) if this select returned zero rows. To ensure your variable changed it's value rewrite it as set expression.
So if you misspelled second threshold name you may be "getting" same #threshold value because second statement does not assign anything to your variable i.e. variable contains value from prior assignment (select). You may test it with additional variable for second threshold - it will be always NULL if i guessed the issue reason.
Also you are applying same #id filter which is a scalar variable. But your rows have different ids. So there is no chances right now to get any other threshold's value than for #id given.
set #threshold = (select t.value
from #ConfigurationTemp t
where t.name='testLimit')
print #threshold
IF #threshold < #funds_limit
RETURN 1000;
set #threshold = (select t.value
from #ConfigurationTemp t
where t.name='testLimit 2')
print #threshold
IF #threshold < #funds_limit
RETURN 1001;
If will succeed only when both arguments are NOT NULL.
One more approach:
declare
#threshold_a int,
#threshold_b int,
#threshold_c int
;with test as
(
select 'a' as name, 25 as value
union all
select 'b', 3
union all
select 'c', 100
union all
select 'd', -1
)
select
#threshold_a = case when t.name = 'a' then t.value else #threshold_a end,
#threshold_b = case when t.name = 'b' then t.value else #threshold_b end,
#threshold_c = case when t.name = 'c' then t.value else #threshold_c end
from test t
select
#threshold_a as [a],
#threshold_b as [b],
#threshold_c as [c]
GO
single select, several variables.

You have RETURN in your IF statment.
... RETURN 1000
and
... RETURN 1001
After insert a row the procedure end.
Maybe you want to assign a result to a variable
#return_Value = ''
#return_Value = #return_Value + '1000, '
....
#return_Value = #return_Value + '1001, '
RETURN #return_Value

Related

I am having the error with a Subquery returning more than one value. How do I reduce to one?

At the very end of the Stored procedure a SELECT statement is made to display the contents of the Table including function that will simultaneously populate fields in the table.
Here is the Select Statement:
IF #type = 'SH'
SELECT DISTINCT *
FROM #History
ORDER BY 1, 2, 3, 4, 5
ELSE
SELECT DISTINCT AmhazName
,Activity
,ServiceName
,Sarid
,PerformedDate
,UserRole
,Details
,dbo.ufn_SarHistoryActionText(sarid, status, performeddate) AS [ActionText]
,FullName
,CategoryDescription
,StatusDescription
,ActionPerformed
,Case
when Details like '%ProjManagerId%'
Then dbo.ufn_GetUserForHistoryReport (PerformedDate, SarId, '%ProjManagerId%')
Else
--when Details like '%UserId%'
dbo.ufn_GetUserForHistoryReport (PerformedDate, SarId, '%UserId%')
--(select 'no user') as [AssignedUser]
End as [AssignedUser]
--,dbo.ufn_GetPMForHistoryReport(PerformedDate, SarId) as [AssignedUser]
FROM #history
ORDER BY 1, 2, 3, 4, 5
DROP TABLE #Historyw
Here is the function I believe is causing problems:
ALTER FUNCTION [dbo].[ufn_SarHistoryActionText]
(
-- Add the parameters for the function here
#sarID int
, #status varchar(6)
, #statusDate datetime
)
RETURNS varchar(100)
AS
BEGIN
-- Declare the return variable here
DECLARE #Result varchar(100)
set #Result = (
SELECT C.ActionText
from LuStatusChange as C
WHERE C.FromStatus = dbo.ufn_SarHistoryPriorStatus(#sarID,#status,#statusDate)
AND C.ToStatus = #status
)
-- Return the result of the function
RETURN #Result
END
GO
As I debug and walk through loads of values, I haven't come across anything that resulted in multiple values. maybe I'm missing something.
Add TOP 1 in the select inside the function:
SELECT TOP 1 C.ActionText
Can you replace
set #Result = (
SELECT C.ActionText
from LuStatusChange as C
WHERE C.FromStatus = dbo.ufn_SarHistoryPriorStatus(#sarID,#status,#statusDate)
AND C.ToStatus = #status
)
as below:
#Result ***IN*** (
SELECT C.ActionText
from LuStatusChange as C
WHERE C.FromStatus = dbo.ufn_SarHistoryPriorStatus(#sarID,#status,#statusDate)
AND C.ToStatus = #status
)
If functionally your query should not written more than 1 row, something is wrong with your query.

Cannot insert the value NULL into column with procedure

This is not a duplicate.
I do understand what the issue means but I don't understand why because the variable contains data. I'm basically trying to make a char(4) column increase alone (just like identity with integers). If the table doesn't contain anything, the first value would be 'C001' otherwise, It simply increase based on the last record.
CREATE PROCEDURE ADD_CL(#nom VARCHAR(20),
#dn DATE)
AS
BEGIN
DECLARE #B CHAR(4)
DECLARE #B_to_int INT
DECLARE #B_new_value CHAR(4)
IF EXISTS(SELECT TOP 1 *
FROM CLIENT)
SET #B_new_value = 'C001'
ELSE
BEGIN
SELECT TOP 1 #B = code_client
FROM client
ORDER BY code_client DESC
SET #B_to_int = CAST(SUBSTRING(#B, 2, 3) AS INTEGER)
SET #B_to_int = #B_to_int + 1;
SET #B_new_value = LEFT(#B, 1) + RIGHT('00' + CAST(#B_to_int AS INT), 3)
END
INSERT INTO CLIENT
VALUES (#B_new_value,
#nom,
#dn)
END
Cannot insert the value NULL into column 'code_client', table 'dbo.CLIENT'; column does not allow nulls. INSERT fails.
#B_new_value represent code_client
Your If Exists should be If Not Exists.
So change
if exists(select TOP 1 * from CLIENT)
to
if not exists(select TOP 1 * from CLIENT)
Also you are adding 00 to your final #B_to_int which is cast as int. so it will show C2,C3 and so on.
If you want to retain the same format, cast it to varchar
SET #B_new_value = LEFT(#B,1) + '00' + CAST(#B_to_int as varchar)
Above line will work only till the count is 9. and then it will continue replicating itself with 1 because 10 will be 0010 and final output will be C0010. To eliminate this issue, use replicate and replicate 0 until 3 characters.
SET #B_new_value = LEFT(#B,1) + REPLICATE('0',3-LEN(#B_to_int)) + #B_to_int
Good Luck.
The other answers already tell you that you should be using NOT EXISTS.
This numbering scheme is quite possibly something you'll regret but you could simplify this a lot as well as making it safer in conditions of concurrency and when you run out of numbers by just doing
CREATE PROCEDURE ADD_CL(#nom VARCHAR(20),
#dn DATE)
AS
BEGIN
DECLARE #B VARCHAR(5);
SET XACT_ABORT ON;
BEGIN TRAN
SELECT #B = FORMAT(1 + RIGHT(ISNULL(MAX(code_client), 'C000'), 3), '\C000')
FROM CLIENT WITH(ROWLOCK, UPDLOCK, HOLDLOCK);
IF ( LEN(#B) > 4 )
THROW 50000, 'Exceeded range',1;
INSERT INTO CLIENT
VALUES (#B,
#nom,
#dn);
COMMIT
END
I believe the following should be 'NOT EXISTS'
if EXISTS(select TOP 1 * from CLIENT)

Function return table variable

I'm trying to create a function that return a table variable.So firstly i get data from Table1 and put it in another table variable. Here i want check if this variable isempty the function return the parameter result else return the result of the table variable
The function script is bellow :
USE[DATABase1]
GO
IF OBJECT_ID (N'CodeFunc', N'TF') IS NOT NULL DROP FUNCTION dbo.CodeFunc;
GO
CREATE FUNCTION CodeFunc ( #Code nvarchar(4) , #Table nvarchar(40) = '' )
RETURNS #VirtualDAT TABLE
(
RowID INT IDENTITY ( 1 , 1 ),
Code nvarchar(400)
)
AS
BEGIN
DECLARE #CodeM nvarchar(400)
DECLARE #imax INT SET #imax = ##ROWCOUNT
DECLARE #i INT SET #i = 1
DECLARE #SelectDAT TABLE
(
RowID INT IDENTITY ( 1 , 1 ),
Code nvarchar(400)
)
INSERT #SelectDAT
SELECT Code FROM table1
WHERE table1.id = 41
IF(EXISTS (SELECT 1 FROM #SelectDAT))
BEGIN
WHILE (#i <= #imax)
BEGIN
SELECT #CodeM = Code FROM #SelectDAT WHERE RowID = #i
INSERT INTO #VirtualDAT(Code) VALUES (#CodeM)
SET #i = #i + 1
END
END
ELSE
INSERT INTO #VirtualDAT(Code) VALUES (#Code)
RETURN
END
So this script works without put it inside function.
And i test this function like this :SELECT * FROM dbo.CodeFunc( 'toto',Default ) the result is :
IF(EXISTS (SELECT 1 FROM #SelectDAT)) no record returned
esle the result is ok
As VR46 says. The ##ROWCOUNT will be set to 0 because there is no query before it. Any code executing in a function happens as a seperate set of queries. It was probably returning a value outside the function because you had previously used the query window for another unrelated query
You could re-factor this function quite dramatically. Look below, ##ROWCOUNT will work here as it is just after the insert query and will definitely have a value based on the insert.
I have not been able to test this, but I think something like this should do the same job.
USE[DATABase1]
GO
IF OBJECT_ID (N'CodeFunc', N'TF') IS NOT NULL DROP FUNCTION dbo.CodeFunc;
GO
CREATE FUNCTION CodeFunc ( #Code nvarchar(4) , #Table nvarchar(40) = '' )
RETURNS #VirtualDAT TABLE
(
RowID INT IDENTITY ( 1 , 1 ),
Code nvarchar(400)
)
AS
BEGIN
insert into #VirtualDAT
Select Code from table1 where table1.id = 41
if ##ROWCOUNT = 0
begin
INSERT INTO #VirtualDAT(Code) VALUES (#Code)
end
RETURN
END
Since you are assigning #imax with ##ROWCOUNT right after declaration of variable will be initialized with zero.
From MSDN ##ROWCOUNT
Returns the number of rows affected by the last statement.
If am not wrong you need to assign value to #imax after the insert into..select query.
INSERT #SelectDAT
SELECT Code FROM table1
WHERE table1.id = 41
SET #imax= ##ROWCOUNT
You can do the same in SET BASED APPROACH without using while loop.
CREATE FUNCTION Codefunc (#Code NVARCHAR(4),
#Table NVARCHAR(40) = '')
returns #VirtualDAT TABLE (
rowid INT IDENTITY ( 1, 1 ),
code NVARCHAR(400))
AS
BEGIN
IF EXISTS (SELECT code
FROM table1
WHERE table1.id = 41)
BEGIN
INSERT INTO #VirtualDAT
(code)
SELECT code
FROM table1
WHERE table1.id = 41
END
ELSE
INSERT INTO #VirtualDAT
(code)
VALUES (#Code)
RETURN
END

IN Clause is not taking dynamic passed value in SQL 2008

I have below SQL Procedure where I am passing and setting dynamic values.
Code: SQL Procedure Name: GetArchivedData
ALTER PROCEDURE [dbo].[GetArchivedData](#PublicationURL varchar(100),#Number int,#Action varchar(max))
AS
DECLARE #TEST Varchar(max)
IF (#Action = 'ALL')
BEGIN
SET #TEST = '''ADD'''+','+'''UPD'''+','+'''DEL''';
END
ELSE
BEGIN
SET #TEST = #Action
END
IF (#Number !=0)
BEGIN
PRINT 'Inside'+ #TEST
BEGIN TRANSACTION TRAN1
SELECT
1 AS Tag,
NULL AS Parent,
NULL AS [root!1!],
NULL AS [Item!2!Id],
NULL AS [Item!2!Action],
NULL AS [Item!2!Publication_Id],
NULL AS [Item!2!Item_Reference_Id],
NULL AS [Item!2!Item_type],
convert( datetime, '9999-01-01' ) AS [Item!2!Last_Published_Date],
NULL AS [Item!2!Url],
NULL AS [Item!2!Schema_Id]
UNION
SELECT TOP (#Number)
2,
1,
'1',
T.ID,
T.ACTION,
T.PUBLICATION_ID,
T.ITEM_REFERENCE_ID,
T.ITEM_TYPE,
T.LAST_PUBLISHED_DATE,
T.URL,
T.SCHEMA_ID
FROM DBO.AUTN_ITEMS T WHERE FLAG=1 AND ACTION IN (#TEST) AND URL LIKE #PublicationURL+'%'
ORDER BY [Item!2!Last_Published_Date] DESC
FOR XML EXPLICIT
COMMIT TRANSACTION TRAN1
END
ELSE IF (#Number = 0)
BEGIN
PRINT 'Outside'+ #TEST
BEGIN TRANSACTION TRAN2
SELECT
1 AS Tag,
NULL AS Parent,
NULL AS [root!1!],
NULL AS [Item!2!Id],
NULL AS [Item!2!Action],
NULL AS [Item!2!Publication_Id],
NULL AS [Item!2!Item_Reference_Id],
NULL AS [Item!2!Item_type],
convert( datetime, '9999-01-01' ) AS [Item!2!Last_Published_Date],
NULL AS [Item!2!Url],
NULL AS [Item!2!Schema_Id]
UNION
SELECT
2,
1,
'1',
T.ID,
T.ACTION,
T.PUBLICATION_ID,
T.ITEM_REFERENCE_ID,
T.ITEM_TYPE,
T.LAST_PUBLISHED_DATE,
T.URL,
T.SCHEMA_ID
FROM DBO.AUTN_ITEMS T WHERE FLAG=1 AND ACTION IN (#TEST) AND URL LIKE #PublicationURL+'%'
ORDER BY [Item!2!Last_Published_Date] DESC
FOR XML EXPLICIT
COMMIT TRANSACTION TRAN2
END
RETURN
Excuting SQL Procedure:
DECLARE #return_value int
EXEC #return_value = [dbo].[GetArchivedData]
#PublicationURL = N'/in',
#Number = 0,
#Action = N'ALL'
SELECT 'Return Value' = #return_value
I can see the values are getting set properly, if I am printing it in the procedure however one value works perfectly but when I am setting SET #TEST = '''ADD'''+','+'''UPD'''+','+'''DEL'''; no results are returned
Please suggest!!
Use Table Variable instead of #Test as string like this,
DECLARE #ActionTbl table ([Action] varchar(3))
IF (#Action = 'ALL')
BEGIN
INSERT INTO #ActionTbl SELECT 'Add' AS ID
UNION ALL
SELECT 'UPD' AS ID
UNION ALL
SELECT 'DEL' ID
END
ELSE
BEGIN
INSERT INTO #ActionTbl VALUES(#Action)
END
And in query use
ACTION IN (Select Action from #ActionTbl)
Instead of
ACTION IN (#TEST)
First of all the main issue here - you CAN'T use IN to search one string in another. IN is used to search value in the row set of values. Also the #TEST STRING value is 'ADD,'UPD','DEL' (each item is with quotas). I guess the Action field contains values without quotas so additional y to other answers there is one more way:
Replace
ACTION IN (#TEST)
with
#TEST LIKE '%'''+ACTION+'''%'

Returning different values based on selected data

I have a stored procedure that selects a row based on an id (simple enough), but only returns the actual result if the data satisfies a few conditions, otherwise it returns specific error codes. So when doing nested checks, the code would look similiar to this:
CREATE PROCEDURE GetStuffById
#StuffId int
AS
BEGIN
IF EXISTS (SELECT TOP 1 * FROM [Stuff] WHERE StuffId = #StuffId)
BEGIN
DECLARE #IsValid bit
SET #IsValid = (SELECT IsValid FROM [Stuff] WHERE StuffId = #StuffId)
IF #IsValid = 1
BEGIN
--More nested checks may occur here
SELECT * FROM [Stuff] WHERE StuffId = #StuffId
END
ELSE
BEGIN
RETURN -2
END
END
ELSE
BEGIN
RETURN -1
END
END
In this approach I already have 3 selects on the same table, which seems redundant and inefficient and another check would mean another select etc. Is there a better pattern to do this (e.g. temp tables)?
UPDATE: edited first check
You can assign to multiple variables in a single select and use ##ROWCOUNT to detect whether a row was found.
DECLARE #IsValid BIT,
#Foo INT
SELECT #IsValid = IsValid,
#Foo = Foo
FROM [Stuff]
WHERE StuffId = #StuffId
/*This must be tested immediately after the assignment statement*/
IF ##ROWCOUNT = 0
RETURN -1
IF ISNULL(#IsValid, 0) = 0
RETURN -2
SELECT #IsValid AS IsValid,
#Foo AS Foo