How to solve the problem that the dynamic query statement is too long to exceed the maximum length of database characters

Source: Internet
Author: User
Last week, a dynamic query statement was used when I wrote the sqlserver2005 database stored procedure. However, because the statement is too long, the variable defined can be varchar (8000) at most. When the string is greater than 8000, the database will be truncated and an error will be reported when a dynamic statement is executed. This problem is not solved. The variable length var is found.

Last week, a dynamic query statement was used when I wrote the SQL server2005 database stored procedure. However, because the statement is too long, the maximum variable defined is varchar (8000 ), when the character string is greater than 8000, the database truncates the string, and an error is returned when a dynamic statement is executed. This problem is not solved. The variable length var is found.

Last week, I wrote SQL server2005DatabaseDuring the stored procedureDynamicQueryStatement, But becauseStatementThe variable is too long. The maximum value of the defined variable is varchar (8000 ).CharacterStringGreater8000,DatabaseWill be truncated and then executedDynamicStatementAn error is reported.


WeiSolutionIn this case,LengthVarchar (max), BasicStatementAs follows:


Declare

@ V_1_str1 varchar (max)
, @ V_1_str2 varchar (8000)


Set @ v_1_str1 = 'select row_number () over (order by a. I _branch_no, a. vc_no) rn
, A. I _branch_no, B. bname as branch_name, a. vc_no
, C. vc_name, a. I _begin_date, a. I _end_date'
Select @ v_1_str1 = @ v_1_str1 + ', sum (case when a. c_relate_type =' + convert (char (5), c_relate_type)
+ 'And a. vc_pro_code =' + convert (varchar (32), vc_pro_code)
+ 'And a. vc_item_code =' + convert (varchar (32), vc_item_code)
+ 'Then isnull (a. en_occur_balance, 0) else 0 end) as '+ title_name
From (select top 500 c_relate_type, vc_pro_code, vc_item_code, title_name
From # check) t
Set @ v_javasstr2 = 'into ## transitem from ## checkit
Left join seclose... branch B on a. I _broker_no = B. bid
Left join sm... tb_operators c on a. vc_no = c. vc_no
Group by a. I _branch_no, B. bname, a. vc_no
, C. vc_name, a. I _begin_date, a. I _end_date'
Exec (@ v_1_str1 + @ v_1_str2 );


Theoretically, after the variable @ v_javasstr1 is defined as varchar (max ),CharacterThe string can be 2 GB, But I didn't define @ v_1_str2 at first, so I used a variable @ v_1_str1, but after execution, I found thatDatabaseError. AfterDynamicQueryStatementTruncation,DynamicSome use the variable @ v_javasstr1, while the static part uses @ v_javasstr2. If the variable is executed again, no error is returned. But in fact, the @ v_javasstr1LengthThe value is still greater than 8000, and the maximum value is visible.CharacterString can be 2 GB, not applicable to all scenarios.


Canghai jiebei

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.