Skip to content
Advertisement

SQL SP is not working giving error Column name or number of supplied values does not match table definition

ALTER PROCEDURE dbo.usp_APP_FORM_GET_ITEMLIST 

    @Table_Name VARCHAR(100),
    @Email_Frequency_INDays VARCHAR(50)

AS
BEGIN

    DECLARE  @RESULTS AS TABLE(Form_Status VARCHAR(100), Created_By VARCHAR(100) )
    DECLARE @query_select AS nvarchar(500) = ' SELECT FORM_STATUS,RECORD_CREATED_BY FROM dbo.' + @Table_Name


      INSERT INTO @RESULTS
       SELECT @query_select

    SELECT *
    FROM @RESULTS;

END
GO

Advertisement

Answer

You need to execute your variable using sp_executesql :

INSERT INTO @RESULTS (Form_Status, Created_By)
EXEC sp_executesql @query_select;

Note : Always qualify column names while using INSERT INTO . ..

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement