patternMinor
User Defined Function Return value
Viewed 0 times
returnuserfunctionvaluedefined
Problem
Hello all I have a function:
but when I execute this function
this gives error:
how to return this executed value? can any one help me please?
ALTER FUNCTION [dbo].[getContentURL]
(
@VID int,
@accountID int
)
RETURNS VARCHAR(500)
AS
BEGIN
DECLARE @cTable varchar(50) = '[CONTENT_CONNECT].[dbo].CONTENT_' + CONVERT(varchar(5),@accountID)
DECLARE @vTable varchar(50) = '[CONTENT_CONNECT].[dbo].VERSION_' + CONVERT(varchar(5),@accountID)
DECLARE @sql VARCHAR(max) = ''
DECLARE @RETURN VARCHAR(500)
DECLARE @ParmDefinition VARCHAR(500)
SET @sql = '
DECLARE @CompanyID int
DECLARE @RETURN VARCHAR(500)
DECLARE @ProjectNumber int
DECLARE @ContentID int
DECLARE @VersionNumber int
DECLARE @FileName varchar(400)
select @VersionNumber= V.Number,@ContentID= V.ContentID,@ProjectNumber=P.Number,@FileName=C.Name,@CompanyID=P.CompanyID
from ' + @vTable + ' V
inner join' + @cTable + ' C on V.ContentID=C.ID
inner join Project P on C.ProjectID=P.ID where V.VID='+convert(varchar,@VID)+'
select ''/''+Convert(varchar,@CompanyID)+''/''+Convert(varchar,@ProjectNumber)+''/''+Convert(varchar,@ContentID)+''/''+Convert(varchar,@VersionNumber)+''/''+@FileName'
SET @ParmDefinition = N'@RETURN VARCHAR(500) OUTPUT'
exec sp_executesql @sql, @ParmDefinition, @RETURN OUTPUT
set @RETURN=(select @RETURN)
RETURN @RETURN
ENDbut when I execute this function
select dbo.getContentURL(177,1)this gives error:
The name 'select --------------------' is not a valid identifier.how to return this executed value? can any one help me please?
Solution
I can't say that I understand what it is that You are trying to do...
The thing is, just like You have already been told, that You can't use Dynamic SQL in a Function.
Using a Stored Procedure will solve the problem You encountered:
(I did not try to find another logic to retrieving the same data, which is possible, I just converted Your Function to a Stored Procedure)
If this does not meet Your needs please post what exactly You are trying to do.
Good Luck,
Roi
The thing is, just like You have already been told, that You can't use Dynamic SQL in a Function.
Using a Stored Procedure will solve the problem You encountered:
(I did not try to find another logic to retrieving the same data, which is possible, I just converted Your Function to a Stored Procedure)
CREATE PROC [dbo].[usp_getContentURL]
(@VID INT, @accountID INT)
AS
DECLARE @cTable varchar(50) = '[CONTENT_CONNECT].[dbo].CONTENT_' + CONVERT(varchar(5),@accountID)
DECLARE @vTable varchar(50) = '[CONTENT_CONNECT].[dbo].VERSION_' + CONVERT(varchar(5),@accountID)
DECLARE @sql VARCHAR(max) = ''
SET @sql = '
DECLARE @CompanyID int
DECLARE @ProjectNumber int
DECLARE @ContentID int
DECLARE @VersionNumber int
DECLARE @FileName varchar(400)
select @VersionNumber=V.Number, @ContentID=V.ContentID, @ProjectNumber=P.Number, @FileName=C.Name, @CompanyID=P.CompanyID
from ' + @vTable + ' V
inner join' + @cTable + ' C on V.ContentID=C.ID
inner join Project P on C.ProjectID=P.ID
where V.VID='+convert(varchar,@VID)+'
select ''/''+Convert(varchar,@CompanyID)
+''/''+Convert(varchar,@ProjectNumber)
+''/''+Convert(varchar,@ContentID)
+''/''+Convert(varchar,@VersionNumber)
+''/''+@FileName'
EXEC (@sql)
GOIf this does not meet Your needs please post what exactly You are trying to do.
Good Luck,
Roi
Code Snippets
CREATE PROC [dbo].[usp_getContentURL]
(@VID INT, @accountID INT)
AS
DECLARE @cTable varchar(50) = '[CONTENT_CONNECT].[dbo].CONTENT_' + CONVERT(varchar(5),@accountID)
DECLARE @vTable varchar(50) = '[CONTENT_CONNECT].[dbo].VERSION_' + CONVERT(varchar(5),@accountID)
DECLARE @sql VARCHAR(max) = ''
SET @sql = '
DECLARE @CompanyID int
DECLARE @ProjectNumber int
DECLARE @ContentID int
DECLARE @VersionNumber int
DECLARE @FileName varchar(400)
select @VersionNumber=V.Number, @ContentID=V.ContentID, @ProjectNumber=P.Number, @FileName=C.Name, @CompanyID=P.CompanyID
from ' + @vTable + ' V
inner join' + @cTable + ' C on V.ContentID=C.ID
inner join Project P on C.ProjectID=P.ID
where V.VID='+convert(varchar,@VID)+'
select ''/''+Convert(varchar,@CompanyID)
+''/''+Convert(varchar,@ProjectNumber)
+''/''+Convert(varchar,@ContentID)
+''/''+Convert(varchar,@VersionNumber)
+''/''+@FileName'
EXEC (@sql)
GOContext
StackExchange Database Administrators Q#24023, answer score: 2
Revisions (0)
No revisions yet.