Whilst the below related to a child stored procedure I believe the issue to be something to do with passing a value within the same procedure..
Issue:
I want to return a value from a user defined function I call from stored procedure.
If I hardcode the line
SELECT #SiteImage = ImageName FROM TblImages WHERE SITEID = #SiteIDOriginal to be
SELECT #SiteImage = ImageName FROM TblImages WHERE SITEID = 'HARDCODED_SITE_ID'then it passes back what I want.
I believe I am passing the correct variable though before but need some guidance as to where it 'drops' the value.
In the 'Parent' stored procedure I request:
dbo.GetPropertyImage(UPPER(#SITEID)) as SitePhoto
Then I do as follows and want to return the value #SiteImage to this line, the passback works as I mentioned I've tested this via hard coding.
USE [cording]
GO
/****** Object: UserDefinedFunction [dbo].[GetPropertyImage] Script Date: 05/18/2015 09:17:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetPropertyImage]
(
-- Add the parameters for the function here
#SiteID varchar(100)
)
RETURNS varchar(100)
AS
BEGIN
-- Declare the return variable here
DECLARE #SiteImage varchar(100)
DECLARE #SiteIDOriginal varchar(100)
-- Add the T-SQL statements to compute the return value here
SELECT #SiteIDOriginal=PemcoShortCode FROM iBasePropertyDetails WHERE PemcoShortCode=#SiteID
SELECT #SiteImage = ImageName FROM TblImages WHERE SITEID = #SiteIDOriginal
--SiteID = #SiteIDOriginal
-- Return the result of the function
RETURN #SiteImage
END
USE [Database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetImage]
(
-- Add the parameters for the function here
#SiteID varchar(100)
)
RETURNS varchar(100)
AS
BEGIN
-- Declare the return variable here
DECLARE #SiteImage varchar(100)
DECLARE #SiteIDOriginal varchar(100)
-- Add the T-SQL statements to compute the return value here
SELECT [#SiteIDOriginal] = ID FROM iBasePropertyDetails WHERE PemcoShortCode=[#SiteID]
SELECT #SiteImage = ImageName FROM TblImages WHERE SITEID = #SiteIDOriginal
-- Return the result of the function
RETURN #SiteImage
END
Related
How do i make the parameter code_gen, give and insert a random value.
#DATE_CREATED datetime = null,
#STATUS varchar(10) = 'Open',
#CODE_GEN as ('TN'+ SELECT LEFT(CONVERT(varchar(100), NEWID()),3))+'-'+RIGHT(CONVERT(varchar(100), NEWID()),5)
as
set nocount on
INSERT INTO MEDREC_CODEGEN (Status,DATE_CREATED,CODE_GEN)
values (#STATUS, COALESCE(#DATE_CREATED,GETDATE()), #CODE_GEN)
You can't use expressions as default values on a stored procedure declaration. You can, however, use them with the SET statement once inside your code.
This fails:
CREATE PROCEDURE MyProcedure
#Parameter INT = (100 + 50) -- Incorrect syntax near '('.
AS
BEGIN
RETURN #Parameter
END
This works:
CREATE PROCEDURE MyProcedure
#Parameter INT = NULL
AS
BEGIN
SET #Parameter = 100 + 50
RETURN #Parameter
END
For your case:
CREATE PROCEDURE YourProcedure
#DATE_CREATED datetime = null,
#STATUS varchar(10) = 'Open',
#CODE_GEN VARCHAR(200) = NULL
as
BEGIN
set nocount on
IF #CODE_GEN IS NULL
SET #CODE_GEN = ('TN'+ SELECT LEFT(CONVERT(varchar(100), NEWID()),3))+'-'+RIGHT(CONVERT(varchar(100), NEWID()),5)
INSERT INTO MEDREC_CODEGEN (Status,DATE_CREATED,CODE_GEN)
values (#STATUS, COALESCE(#DATE_CREATED,GETDATE()), #CODE_GEN)
END
SQL Server accepts only literals as parameter defaults.
default
A default value for a parameter. If a default value is defined
for a parameter, the procedure can be executed without specifying a
value for that parameter. The default value must be a constant or it
can be NULL..
Change it into a SET statement inline, perhaps with an ISNULL if you set the default to NULL.
Use this may be help
DECLARE #DATE_CREATED AS DATETIME = NULL
DECLARE #STATUS AS varchar(10) = 'Open'
DECLARE #CODE_GEN as varchar(10) = NULL
SET #CODE_GEN = ('TN'+ (SELECT LEFT(CONVERT(varchar(100), NEWID()),3))+'-
'+RIGHT(CONVERT(varchar(100), NEWID()),5))
set nocount on
INSERT INTO MEDREC_CODEGEN (Status,DATE_CREATED,CODE_GEN)
values (#STATUS, COALESCE(#DATE_CREATED,GETDATE()), #CODE_GEN)
Here what I did
create table #Temp
(
Status Varchar(50),
DATE_CREATED Varchar(50),
CODE_GEN Varchar(50)
)
DECLARE #DATE_CREATED AS DATETIME = NULL
DECLARE #STATUS AS varchar(10) = 'Open'
DECLARE #CODE_GEN as varchar(10) = NULL
SET #CODE_GEN = ('TN'+ (SELECT LEFT(CONVERT(varchar(100), NEWID()),3))+'-
'+RIGHT(CONVERT(varchar(100), NEWID()),5))
set nocount on
INSERT INTO #Temp (Status,DATE_CREATED,CODE_GEN)
values (#STATUS, COALESCE(#DATE_CREATED,GETDATE()), #CODE_GEN)
Select * FROM #Temp
Result :
Status DATE_CREATED CODE_GEN
Open Apr 4 2018 12:23PM TN437-
I am sure the solution is something super simple that I am missing but I keep getting a
SqlException: Procedure or function expects parameter which was not supplied
error. I am not a SQL wizard but to me the parameter looks okay. I did change the parameter and was not receiving this error but then when I consistently started receiving it I restored the stored procedure to the original version that I knew for a fact was fine but still receive it.
I tried executing the stored procedure like this
EXECUTE [dbo].[BHS_CloseCnt_Print_PackList] #palletid = '562992'
with a variable filled in. This stored procedure calls a function that determines the status of an order, if the variable I plug in and check with this method meets the criteria for the function I get an expected return.
If the container does not yet meet the function criteria, I get a null which I believe is okay.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[BHS_CloseCnt_Print_PackList]
(#PalletId numeric)
AS
BEGIN
Declare #PO as nvarchar(50)
Declare #Internal_Shipment_Num as numeric
Declare #Internal_Shipment_Line_Num as numeric
select top 1
#Internal_Shipment_Num = sc.INTERNAL_SHIPMENT_NUM,
#Internal_Shipment_Line_Num = sc.INTERNAL_SHIPMENT_LINE_NUM
from
SHIPPING_CONTAINER sc
where
INTERNAL_CONTAINER_NUM = #PalletId
or PARENT = #PalletId
and INTERNAL_SHIPMENT_LINE_NUM is not null
select #PO = dbo.fn_BHS_AllPOPLTS_CLOSED(#PalletId, #Internal_Shipment_Num, #Internal_Shipment_Line_Num)
print #PO
if #PO is not null
Begin
select #PalletId 'INTERNAL_CONTAINER_NUM', '60' 'DOCUMENT_TYPE'
End
End
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_BHS_AllPOPLTS_CLOSED]
(#palletId numeric,
#Internal_Shipment_Num numeric,
#Internal_Shipment_Line_Num numeric)
RETURNS nvarchar(50)
AS
BEGIN
Declare #PO nvarchar(50)
Declare #OPENPO nvarchar(50)
Declare #IntShip as numeric
select #PO = isnull(sd.CUSTOMER_PO, 'FEIT')
from SHIPMENT_DETAIL sd
where sd.INTERNAL_SHIPMENT_LINE_NUM = #Internal_Shipment_Line_Num
and sd.internal_shipment_num = #Internal_Shipment_Num
select #OPENPO = isnull(sd.CUSTOMER_PO, '')
from shipping_container sc
join SHIPMENT_DETAIL sd on sd.INTERNAL_SHIPMENT_LINE_NUM = sc.INTERNAL_SHIPMENT_LINE_NUM
where sd.CUSTOMER_PO = #PO and sc.INTERNAL_SHIPMENT_NUM = #Internal_Shipment_Num
and sc.status < 600
if(isnull(#OPENPO, '') != '')
Begin
set #PO = null
End
return #PO
End
The stored procedure looks to have stored the cache from the previous edit I did although a known working version was restored.
Resolution for this was to run DBCC FREEPROCCACHE to clear the stored procedure cache and I was able to execute as expected.
Thanks Nemanja Perovic!
I have a stored procedure which contains a null comparison of
--cutting the declaration of various variables
SELECT #val = id FROM dbo.example_table
WHERE type=#type AND date=#date
if #val = null
insert record
When this is executed in the stored procedure with an empty table, this will insert the record, however when this is executed as a block of code outside the stored proc, it will not perform the insert. There are also other SQL servers on which the insert will never be made, whether inside or outside the stored proc.
Is there some explanation for why his can work in some situations but not in others? I know the correct way to perform the comparison is #val IS NULL, but i was curious to to whether #val = null was documented anywhere for completeness.
Check the setting for ANSI_NULLS.
This would return a false:
declare #var varchar(10) = null;
SET ANSI_NULLS ON
if (#var = null) select 'true';
else select 'false';
This would return a true:
declare #var varchar(10) = null;
SET ANSI_NULLS OFF
if (#var = null) select 'true';
else select 'false';
I written the following function.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create FUNCTION NameFunction
(
#eid int
)
RETURNS varchar
AS
BEGIN
Declare #logid varchar(50);
SELECT #logid = E.LoginId from HumanResources.Employee As E
where E.BusinessEntityID = #eid
RETURN #logid
END
GO
When I am executing it is showing result as a.
But expected result is adventure-works\terri0
Where I did the mistake here. Only first character coming. Need to change any thing?
Change your RETURN type to include a length, at this point it is just returning 1 character:
RETURNS varchar(100)
Full code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create FUNCTION NameFunction
(
#eid int
)
RETURNS varchar(100) -- or whatever length you need
AS
BEGIN
Declare #logid varchar(50);
SELECT #logid = E.LoginId from HumanResources.Employee As E
where E.BusinessEntityID = #eid
RETURN #logid
END
GO
RETURNS varchar should be RETURNS varchar(50).
varchar without a length specified is interpreted as varchar(1) in this context (and as varchar(30) in the context of a CAST).
BTW: Scalar UDFs that do data access can be performance killers. You might want to consider at least rewriting this as an inline TVF so that the optimiser has more options.
I would like to know if in SQL is it possible to return a varchar value from a stored procedure, most of the examples I have seen the return value is an int.
Example within a procedure:
declare #ErrorMessage varchar(255)
if #TestFlag = 0
set #ErrorMessage = 'Test'
return #ErrorMessage
You can use out parameter or the resulset to return any data type.
Return values should always be integer
CREATE PROCEDURE GetImmediateManager
#employeeID INT,
#managerName VARCHAR OUTPUT
AS
BEGIN
SELECT #managerName = ManagerName
FROM HumanResources.Employee
WHERE EmployeeID = #employeeID
END
Taken from here
You will need to create a stored function for that:
create function dbo.GetLookupValue(#value INT)
returns varchar(100)
as begin
declare #result varchar(100)
select
#result = somefield
from
yourtable
where
ID = #value;
return #result
end
You can then use this stored function like this:
select dbo.GetLookupValue(4)
Marc
A stored procedure's return code is always integer, but you can have OUTPUT parameters that are any desired type -- see http://msdn.microsoft.com/en-us/library/aa174792.aspx .