How to pass a string larger than 200 character to a stored procedure via param

后端 未结 2 1278
别那么骄傲
别那么骄傲 2021-01-29 09:28

I got stuck with one problem, in my code i have to make a sum request of all article that is present in my datatable, i concatenate all article ID in one string like \'a1,a2,a3\

相关标签:
2条回答
  • 2021-01-29 10:22

    I had p.Size = 200; inside my method that is adding the Sql parameters, i had to change it to p.Size = 8000; and it's working.

    Thanks everybody for the help !

    0 讨论(0)
  • 2021-01-29 10:34

    I agree with Sean Lange's comment, and would suggest to use a table valued parameter instead of sending a string and parsing it in sql
    To do that, you need to create a user defined table type in your sql server:

    CREATE TYPE dbo.ArticleIds as table
    (
        Id varchar(10) -- should be the same as Code_article definition!
    )
    GO
    

    and then use it as

    ALTER PROCEDURE [dbo].[GetChargePetrin]
    -- Add the parameters for the stored procedure here
        @articlesList dbo.ArticleIds readonly -- Must be readonly!
    AS
    
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
            SELECT 
            CAST(SUM(CAST(Qa01 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa1',
            CAST(SUM(CAST(Qa02 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa2',
            -- ... more of the same
            CAST(SUM(CAST(Qa60 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa60'
            FROM [PDP_TTP].[dbo].[PDP] p
            INNER JOIN [PDP_TTP].[dbo].[Articles] a ON a.Division=p.Division AND a.Code_article=p.Code_article
            INNER JOIN @articlesList al ON a.Code_article = al.Id
            WHERE CAST(a.CO_UQB_PET AS INT) > 0 
    
    END
    

    To execute a stored procedure with a table valued parameter from c# using ADO.NET, you need to send a parameter with type SqlDbType.Structured and pass a DataTable as it's value.

    0 讨论(0)
提交回复
热议问题