2017-01-26 5 views
-1

私のコードでは、私のデータテーブルに存在するすべての記事の合計要求を作成する必要があります。すべてのアーティクルIDを連結します'a1、a2、a3'のような1つの文字列で、これは動作するはずです。200文字を超える文字列をparam経由でストアドプロシージャに渡す方法

しかし私は大きなIDと約150の記事を持っているので、ストアドプロシージャに渡そうとする文字列は約1300文字です。これはストアドプロシージャに行くときに200文字で切り捨てられます。

この文字列を切り捨てるためにSQL Serverを使用せずに大きな文字列をストアドプロシージャに渡す方法はありますか?

私はここでC#コードまたはSQLストアドプロシージャを書くことができます。ここで

がストアドプロシージャである:私は、SQLのパラメータを追加して、私のメソッド内p.Size = 200;を持っていた

ALTER PROCEDURE [dbo].[GetChargePetrin] 
-- Add the parameters for the stored procedure here 
@articlesList varchar(MAX) 
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', 
     CAST(SUM(CAST(Qa03 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa3', 
     CAST(SUM(CAST(Qa04 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa4', 
     CAST(SUM(CAST(Qa05 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa5', 
     CAST(SUM(CAST(Qa06 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa6', 
     CAST(SUM(CAST(Qa07 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa7', 
     CAST(SUM(CAST(Qa08 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa8', 
     CAST(SUM(CAST(Qa09 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa9', 
     CAST(SUM(CAST(Qa10 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa10', 
     CAST(SUM(CAST(Qa11 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa11', 
     CAST(SUM(CAST(Qa12 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa12', 
     CAST(SUM(CAST(Qa13 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa13', 
     CAST(SUM(CAST(Qa14 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa14', 
     CAST(SUM(CAST(Qa15 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa15', 
     CAST(SUM(CAST(Qa16 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa16', 
     CAST(SUM(CAST(Qa17 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa17', 
     CAST(SUM(CAST(Qa18 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa18', 
     CAST(SUM(CAST(Qa19 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa19', 
     CAST(SUM(CAST(Qa20 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa20', 
     CAST(SUM(CAST(Qa21 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa21', 
     CAST(SUM(CAST(Qa22 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa22', 
     CAST(SUM(CAST(Qa23 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa23', 
     CAST(SUM(CAST(Qa24 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa24', 
     CAST(SUM(CAST(Qa25 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa25', 
     CAST(SUM(CAST(Qa26 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa26', 
     CAST(SUM(CAST(Qa27 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa27', 
     CAST(SUM(CAST(Qa28 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa28', 
     CAST(SUM(CAST(Qa29 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa29', 
     CAST(SUM(CAST(Qa30 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa30', 
     CAST(SUM(CAST(Qa31 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa31', 
     CAST(SUM(CAST(Qa32 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa32', 
     CAST(SUM(CAST(Qa33 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa33', 
     CAST(SUM(CAST(Qa34 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa34', 
     CAST(SUM(CAST(Qa35 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa35', 
     CAST(SUM(CAST(Qa36 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa36', 
     CAST(SUM(CAST(Qa37 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa37', 
     CAST(SUM(CAST(Qa38 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa38', 
     CAST(SUM(CAST(Qa39 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa39', 
     CAST(SUM(CAST(Qa40 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa40', 
     CAST(SUM(CAST(Qa41 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa41', 
     CAST(SUM(CAST(Qa42 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa42', 
     CAST(SUM(CAST(Qa43 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa43', 
     CAST(SUM(CAST(Qa44 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa44', 
     CAST(SUM(CAST(Qa45 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa45', 
     CAST(SUM(CAST(Qa46 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa46', 
     CAST(SUM(CAST(Qa47 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa47', 
     CAST(SUM(CAST(Qa48 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa48', 
     CAST(SUM(CAST(Qa49 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa49', 
     CAST(SUM(CAST(Qa50 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa50', 
     CAST(SUM(CAST(Qa51 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa51', 
     CAST(SUM(CAST(Qa52 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa52', 
     CAST(SUM(CAST(Qa53 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa53', 
     CAST(SUM(CAST(Qa54 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa54', 
     CAST(SUM(CAST(Qa55 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa55', 
     CAST(SUM(CAST(Qa56 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa56', 
     CAST(SUM(CAST(Qa57 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa57', 
     CAST(SUM(CAST(Qa58 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa58', 
     CAST(SUM(CAST(Qa59 AS INT) /CAST(a.CO_UQB_PET AS INT)) AS varchar) as 'qa59', 
     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 
     WHERE CAST(a.CO_UQB_PET AS INT) >0 and a.Code_article IN(SELECT Id = Item FROM dbo.SplitInts(@articlesList, ',')); 

END 
+10

保存されたprocを確認する必要があります。おそらくparamは 'varchar(200)'であり、その答えは明らかです! – Jamiec

+4

区切られた文字列を渡す代わりに、テーブル値のパラメータを調べる必要があります。区切り文字列を渡すときには、それらを分割するか、動的SQLを介してSQLインジェクションの脆弱性を公開する必要があります。 –

+0

格納されたprocがvarchar(200)型のパラメータを受け入れる場合、入力値を制限値に切り捨てます。そうであれば、そのパラメータの容量をあなたの要件に合ったより大きなものに変更する必要があります。また、適切な容量の列も持つデータを挿入する表を確認してください。 –

答えて

3

私はSean Langeのコメントに同意し、文字列を送信してSQLで解析する代わりに、テーブル値のパラメータを使用することをお勧めします
これを行うには、SQL Serverでユーザー定義のテーブルタイプを作成する必要があります。

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

して、ADO.NETを使ってC#からパラメータを大切にテーブルとストアドプロシージャを実行するには

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 

としてそれを使用し、必要にタイプSqlDbType.Structuredのパラメータを送信し、値としてDataTableを渡します。

+0

解決策を使用します。最初は、将来500件以上の記事があることを考えなかったので、その数の記事を処理できるようにソリューションが必要です。ありがとう –

+0

私はちょうどあなたのソリューションの実装とテストを完了し、それは偉大な作業、私はSQLの例で最高のソリューションを表示するための時間を取ってくれて、あなたは私の日を救った! –

0

、私はp.Size = 8000;にそれを変更しなければならなかったし、それが働いています。

ありがとうございました!

+0

これは8,000文字を超えるまで有効です。スケーラビリティ、パフォーマンス、メンテナンス性の方が、このプロセスを構築する方がはるかに優れているので、いくつの値を持つかは関係ありません。これは、テーブル値パラメータが本当に優れている場所です。 https://msdn.microsoft.com/en-us/library/bb510489.aspx –

関連する問題