Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要在SQL Server字符串函数中使用变量_Sql_Sql Server_Tsql_Query String - Fatal编程技术网

需要在SQL Server字符串函数中使用变量

需要在SQL Server字符串函数中使用变量,sql,sql-server,tsql,query-string,Sql,Sql Server,Tsql,Query String,我不是一个优秀的SQL用户,我希望解决一个简单的问题。虽然我最终需要“循环”一个字符串列表,该列表是单列查询的结果,但我首先需要解决这个问题: 我想在SQLServer字符串函数中使用一个变量。下面是一段示例代码: declare @STR nvarchar(50) set @STR = 'ceo' SELECT Document_Text.DocText_ID, SUBSTRING(Document_Text.DocText, CHARINDEX(@STR, Document_

我不是一个优秀的SQL用户,我希望解决一个简单的问题。虽然我最终需要“循环”一个字符串列表,该列表是单列查询的结果,但我首先需要解决这个问题:

我想在SQLServer字符串函数中使用一个变量。下面是一段示例代码:

declare @STR nvarchar(50)

set @STR = 'ceo'

SELECT
   Document_Text.DocText_ID,
   SUBSTRING(Document_Text.DocText, CHARINDEX(@STR, Document_Text.DocText)-125, 250) as SubText
FROM
   Document_Text
WHERE
   Document_Text.DocText like '%@STR%'
如上所述,在这样的查询中,我最终将使用单列查询(~200个值)的结果来代替@STR

从应用的观点出发,考虑如下:

  • Document_Text包含UserID、DocID、DocURL和DocText列,最后一列是简历的文本形式。STR是一个简短的查询词-nvarchar(50)-可以在上面搜索(我知道作为一个搜索应用程序,这可能看起来不直观,但是还有另一种形式的点击处理,它使用这个查询的结果“使它有意义”)
  • 我的最终查询将从同一数据库中另一个表的单个列中获取作业角色或功能区域关键字
如果有人对下一步有任何建议,我也将不胜感激。谢谢

declare @STR nvarchar(50)

set @STR = 'ceo'

SELECT
   Document_Text.DocText_ID,
   SUBSTRING(Document_Text.DocText, CHARINDEX(@STR, Document_Text.DocText)-125, 250) as SubText
FROM
   Document_Text
WHERE
   Document_Text.DocText like '%'+@STR+'%'
OPTION (RECOMPILE)
我包括了表变量,以便您可以看到我的想法。
这就是你要找的吗。它避免了游标,但我不知道它的伸缩性有多好。

为此,您需要使用全文索引。如果您正在考虑循环,您还没有进入SQL思维模式:-)虽然语言“SQL”的官方发音为“essquelle”,但产品“SQL Server”的发音为“sequel Sir Vurr”,因此它应该是“SQL Server字符串函数”(而不是“an”)。不过,可能不值得编辑;)希望按照Tim Franklin下面的解释加入。。。同意循环是循环的。而且。。。简历文本已经被推送到一个大的nvarchar(MAX)字段中。我确实做了全文索引,但当我进一步思考这个问题时,我尝试看看LIKE%@STR%是否会给我与CONTAINS相同的结果。在这种特殊情况下,它似乎有。。。进一步的黑客攻击将进行最终验证。谢谢你的提醒。好的,这一个对最初的案例有效。谢谢我应该更深入地研究表达能力建设。我的一个朋友也提出了同样的建议。。。但他没有提供完整的例子!所以你大妈!我的“新手主义”不允许我对你的解决方案竖起大拇指。
DECLARE   @Document_Text TABLE(UserID INT , DocText_ID INT , DocURL NVARCHAR(100),DocText NVarchar(4000))
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES (1,1,'Blah','Ceo blah blah blah blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (2,1,'Blah','blah blah blah blah blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (3,1,'Blah','blah blah Manager blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (4,1,'Blah','blah blah blah blah blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (5,1,'Blah','blah blah blah blah blah time time time') 
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (6,1,'Blah','Cblah blah blah blah blah time time time') 
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (7,1,'Blah','Cblah blah blah blah blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (8,1,'Blah','blah Ceo blah blah blah blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (9,1,'Blah','blah blah blah blah blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (10,1,'Blah','blah blah Programmer blah blah time time time' )
INSERT INTO @Document_Text (UserID , DocText_ID , DocURL,DocText) VALUES  (11,1,'Blah','blah blah blah blah blah time time time' )


DECLARE  @JobRole TABLE (JR_ID INT , JobRole NVARCHAR(50))
INSERT INTO @Jobrole (JR_ID , JobRole ) VALUES (1,'Programmer') 
INSERT INTO @Jobrole (JR_ID , JobRole ) VALUES (2,'Ceo') 
INSERT INTO @Jobrole (JR_ID , JobRole ) VALUES (3,'Manager') 



 SELECT
   JobRole,DT.DocText_ID,
   SUBSTRING(DT.DocText, CHARINDEX(JobRole, DT.DocText)-125, 250) as SubText
FROM
   @Document_Text DT CROSS JOIN  @JobRole JR 
WHERE
   DT.DocText like '%'+ JobRole +'%''