Execute a User Defined Function (String_SPLIT) from a Stored Procedure. SQL SERVER

0

I have a table 'interviews' in SQL that stores, among others, NVARCHAR data in the column 'account_id', and data type NTEXT in the column 'answer' which is fed from a series of concatenated questions and answers divided by the symbol '|'.

I already have a function that separates strings (works correctly when entering plain text), its variables are ( @string NVARCHAR ,@simbolo CHAR(1) ); What I am trying to do (and I do not get) is that from a Stored Procedure look in the table 'interviews' the answers given by the account ###, then take that value and through the function, I break down a table with each section divided by '|'. This is what I have:

a) The function:

CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END

b) The procedure:

    CREATE PROCEDURE [dbo].[respuestaSPLIT]
(                                               
@cuenta NVARCHAR(max),                  
@ans NVARCHAR(max)                      
)                                       
as                                      
    SELECT respuesta FROM [pruebaEDGE].[dbo].[entrevistas] WHERE [id_cuenta]= '@cuenta'
    set @ans = respuesta
    SELECT * FROM [dbo].[fnSplitString] (@ans,'|')
Go

c) the table:

'CREATE TABLE [dbo].[entrevistas](
    [id_entrevista] [int] IDENTITY(1,1) NOT NULL,
    [id_cuenta] [nvarchar](max) NOT NULL,
    [tipo_entrevista] [int] NOT NULL,
    [numero_entrevistado] [int] NOT NULL,
    [nombre_entrevistado] [nvarchar](max) NULL,
    [respuesta] [ntext] NULL,
    [fecha_alta] [date] NULL,
    [id_usuario_alta] [int] NULL,
    [fecha_modificacion] [date] NULL,'

d) the error:

Msg 207, Level 16, State 1, Procedure respuestaSPLIT, Line 8 [Batch Start Line 0]
Invalid column name 'nombre_entrevistado'.
    
asked by Edgard Huerta 22.05.2017 в 22:25
source

1 answer

1

Well, the procedure should be as follows:

CREATE PROCEDURE [dbo].[respuestaSPLIT]
(                                               
@cuenta NVARCHAR(max)                     
)                                       
as                                      
DECLARE @ans NVARCHAR(max);

SELECT @ans = respuesta 
FROM [pruebaEDGE].[dbo].[entrevistas] 
WHERE [id_cuenta]= @cuenta;

SELECT * 
FROM [dbo].[fnSplitString] (@ans,'|');

Anyway I do not understand why you are creating this sp instead of just using the funcipón directly

    
answered by 22.05.2017 / 22:59
source