SQL 需要暂时将 Text 字段转换为 Varchar,以便我可以传递给存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4360150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:29:20  来源:igfitidea点击:

Need to convert Text field to Varchar temporarily so that I can pass to a stored procedure

sqlsql-servertsqlsql-server-2000

提问by user53885

I am using a SQL 2000 database.

我正在使用 SQL 2000 数据库。

I am working with a database in which I cannot change the types on the tables, or the stored procedures. One of the stored procedures I need to call expects a parameter of 'text'. I can get to the text field, but I am unable to figure out who to store that in a variable or any other way to pass it into the stored procedure?

我正在使用一个无法更改表类型或存储过程的数据库。我需要调用的存储过程之一需要“文本”参数。我可以进入文本字段,但我无法弄清楚谁将其存储在变量中或以任何其他方式将其传递给存储过程?

If I try and create a text variable, SQL won't let me - if I convert it to varchar I only get the first character from the text field.

如果我尝试创建一个文本变量,SQL 不会让我 - 如果我将其转换为 varchar,我只能从文本字段中获取第一个字符。

Any tricks to get around this much appreciated! Thank you!

任何解决这个问题的技巧都非常感谢!谢谢!

回答by Martin Smith

Declare the variable of type varchar(8000)

声明类型变量 varchar(8000)

declare @v varchar(8000)
SET @v = (SELECT CAST(textcol as varchar(8000)) FROM yourtable WHERE ....)

Obviously it might still be truncated but not at 1 character.

显然它可能仍然被截断,但不是 1 个字符。