如何执行超出长度的查询

时间:2019-06-13 05:33:09

标签: sql sql-server tsql

以'“ 0111”或“ 0112”或“ 0115”或'开头的字符串太长。最大长度为4000。

+----+------------------+----------+---------------------------+
| id | special_event_id | place_id | created_at                |
+----+------------------+----------+---------------------------+
| 1  | 2                | 243      | 2015-10-12 18:07:09 +0100 |
| 3  | 2                | 83       | 2015-10-15 15:54:40 +0100 |
| 5  | 4                | 262      | 2015-11-26 16:29:35 +0000 |
| 4  | 3                | 262      | 2015-11-23 16:25:31 +0000 |
| 6  | 5                | 281      | 2016-02-24 15:20:33 +0000 |
| 7  | 6                | 262      | 2016-03-29 14:34:00 +0100 |
| 8  | 7                | 262      | 2016-04-11 13:28:00 +0100 |
| 9  | 8                | 262      | 2016-06-09 15:03:52 +0100 |
| 12 | 11               | 262      | 2016-07-11 17:20:26 +0100 |
| 10 | 9                | 262      | 2016-06-09 15:20:08 +0100 |
+----+------------------+----------+---------------------------+

2 个答案:

答案 0 :(得分:0)

尝试一下:

declare @stmt1 varchar(max)
set @stmt1=CAST('insert into dbo.abcd SELECT [company],[address], 
[address2],[zip],[zip4],[city],[state], 
[telephone_number],LOWER([email]),[name],[fname],[mname],[lname], 
[title_full] FROM DBO.filter WHERE  
[id] in (''7'',''8'',''11'',''15'')  and contains 
(code,' AS VARCHAR(MAX))+''''+'"0111" OR "4142"......................OR 
"5999"'+''''+')'


exec (@stmt1) AT [server]

答案 1 :(得分:0)

假设您的代码列表存储在一个表中,例如'dbo.codes',并且字段'code'包含该值,则可以避开CONTAINS谓词,并使用LIKE在两个表之间使用INNER JOIN。 / p>

DECLARE @stmt1 VARCHAR(MAX)
SET @stmt1 = 'INSERT INTO dbo.abcd
SELECT DISTINCT [F].[company], [F].[address], [F].[address2], [F].[zip], [F].[zip4],
[F].[city], [F].[state], [F].[telephone_number], LOWER([F].[email]),
[F].[name], [F].[fname], [F].[mname], [F].[lname], [F].[title_full]
FROM dbo.filter AS F INNER JOIN dbo.codes AS C ON F.code LIKE ''%'' + C.code + ''%''
WHERE (F.id in (''7'',''8'',''11'',''15''))';
EXEC (@stmt1) AT [server]