hidrau
asked on
how to use a store procedure into a function ? what is it wrong in my code?
Hello guys,
I am trying to do this, but MSSQL is complaining about my syntax, I need your help.
Thanks
Alex
I am trying to do this, but MSSQL is complaining about my syntax, I need your help.
create function dbo.LeFormula(@codpro varchar(24),
@codespec varchar(5),
@formula varchar(500),
@CodcampoFormula varchar(100))
returns varchar(100)
as
begin
declare @valor as varchar(100);
select @valor= Exec sp_CALCULO_FORMULA_ESPEC @codpro, @codespec, '', '', '', '', '', @formula, @CodcampoFormula
return @valor
End
Thanks
Alex
Pls try this -
You have to insert data in a temp variable and then you assign that to a variable and then return. I have changed code for you.
You have to insert data in a temp variable and then you assign that to a variable and then return. I have changed code for you.
create function dbo.LeFormula(@codpro varchar(24),
@codespec varchar(5),
@formula varchar(500),
@CodcampoFormula varchar(100))
returns varchar(100)
as
begin
declare @valor1 as varchar(100)
DECLARE @t TABLE(valor varchar(100))
INSERT INTO @t(valor)
Exec sp_CALCULO_FORMULA_ESPEC @codpro, @codespec, '', '', '', '', '', @formula, @CodcampoFormula
SELECT @valor1 = valor FROM @t
return @valor1
End
ASKER
I got this Pawan Kumar
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.
Sorry my bad , you cannot call a SP inside a function.
you can embed you sp code inside the function as a solution.
you can embed you sp code inside the function as a solution.
Please see if this works for you-
create function dbo.LeFormula(@codpro varchar(24),
@codespec varchar(5),
@formula varchar(500),
@CodcampoFormula varchar(100))
returns varchar(100)
as
begin
declare @valor1 as varchar(100)
DECLARE @t TABLE(valor varchar(100))
INSERT INTO @t(valor)
SELECT * from OPENROWSET('SQLNCLI10', 'Server=<SERVERNAME>;UID=<LOGIN>;Pwd=<PASSWORD>;','Exec sp_CALCULO_FORMULA_ESPEC @codpro, @codespec, '''', '''', '''', '''', '''', @formula, @CodcampoFormula') AS C
SELECT @valor1 = valor FROM @t
return @valor1
End
ASKER
is it possible only with OPENROWSET ?
I have not tried with OpenRowSet. I think i may work.
You can try.
You can try.
ASKER
the problem is the password, login name inside de code.
All my problem is because I need to get the value from my store proc and retrieve it in my query, to solve it I thought to call it through a function.
All my problem is because I need to get the value from my store proc and retrieve it in my query, to solve it I thought to call it through a function.
You can use a stored procedure.
ASKER
In my query? How?
ASKER
I have tried this but it doesn't allow it
SELECT
Relat_PCP.NUMPED, Relat_PCP.SEQPED, Relat_PCP.DATPED, Relat_PCP.OBS, Relat_PCP.NOMCAD, Relat_PCP.NOMFAN, Relat_PCP.DATPREV_FINALIZACAO,
Relat_PCP_Itens.CODPRO, Relat_PCP_Itens.NOMPRO, Relat_PCP_Itens.QTDPROPROD, Relat_PCP_Itens.LOTE, Relat_PCP_Itens.NOMTIPPRO, Relat_PCP_Itens.NOMAPLICA,
(select NOMFAM from arest A INNER JOIN ArFAM B ON B.CODFAM=A.CODFAM where A.Codpro=Relat_PCP_Itens.CODPRO),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'LARGURA1')),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'MEIO')),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'LARGURA2')),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'ESPESSURA')),
(Exec sp_CALCULO_FORMULA_ESPEC '000990','001', '', '', '', '', '', '((MEIO@)*(LARGURACHAPA@)*(ESPESSURA@)*(PESPECIFICO@)*(OBSIMPRESSAO@))', 'KG')
FROM
Relat_PCP Relat_PCP,
Relat_PCP_Itens Relat_PCP_Itens
WHERE
Relat_PCP.NUMPED = Relat_PCP_Itens.NUMPED AND
Relat_PCP.SEQPED = Relat_PCP_Itens.SEQPED AND
Relat_PCP.NUMPED = 4410 AND
Relat_PCP.SEQPED = 0
Please try this - this should work.
DECLARE @t TABLE(valor varchar(100))
INSERT INTO @t(valor)
Exec sp_CALCULO_FORMULA_ESPEC '000990','001', '', '', '', '', '', '((MEIO@)*(LARGURACHAPA@)*(ESPESSURA@)*(PESPECIFICO@)*(OBSIMPRESSAO@))', 'KG'
SELECT
Relat_PCP.NUMPED, Relat_PCP.SEQPED, Relat_PCP.DATPED, Relat_PCP.OBS, Relat_PCP.NOMCAD, Relat_PCP.NOMFAN, Relat_PCP.DATPREV_FINALIZACAO,
Relat_PCP_Itens.CODPRO, Relat_PCP_Itens.NOMPRO, Relat_PCP_Itens.QTDPROPROD, Relat_PCP_Itens.LOTE, Relat_PCP_Itens.NOMTIPPRO, Relat_PCP_Itens.NOMAPLICA,
(select NOMFAM from arest A INNER JOIN ArFAM B ON B.CODFAM=A.CODFAM where A.Codpro=Relat_PCP_Itens.CODPRO),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'LARGURA1')),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'MEIO')),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'LARGURA2')),
(select dbo.fn_PegaDados_EspTec_Arest_Crystal(Relat_PCP_Itens.CODPRO, isnull(Relat_PCP_Itens.LOTE,'') , 'ESPESSURA')),
(SELECT valor FROM @t)
FROM
Relat_PCP Relat_PCP,
Relat_PCP_Itens Relat_PCP_Itens
WHERE
Relat_PCP.NUMPED = Relat_PCP_Itens.NUMPED AND
Relat_PCP.SEQPED = Relat_PCP_Itens.SEQPED AND
Relat_PCP.NUMPED = 4410 AND
Relat_PCP.SEQPED = 0
ASKER
This way as you told me, works fine.
But I can't use this way because this query is used in my crystal reports version 8.5
I am also trying to find another way,
But I can't use this way because this query is used in my crystal reports version 8.5
I am also trying to find another way,
ASKER
is it possible to create a view with that code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah, you gave me a good ideia. I'll do that
ASKER
thanks
For example, you should not change the state of the database, etc. As such executing a sp with function might be prohibited as it cannot know what a sp is doing in turn.
Please go through all restrictions at following link:
https://docs.microsoft.com/en-us/sql/relational-databases/user-defined-functions/create-user-defined-functions-database-engine