简体   繁体   English

SQL Server可以在运行时进行类型转换吗

[英]Can SQL Server do a type conversion at run-time

We have a utility written in C which reads columns extracted from a database using a stored procedure and outputs a csv file. 我们有一个用C编写的实用程序,该实用程序使用存储过程读取从数据库中提取的列并输出一个csv文件。 Simple huh. 简单吧。 However when reading a smallint column, it crashes out and not being the greatest C programmer on the planet, I can't nail it. 但是,当阅读smallint列时,它崩溃了并且不是地球上最伟大的C程序员,我无法钉牢它。 As a workaround can you change the data type in a stored procedure eg could the C program "see" the column as a varchar rather than a smallint at runtime? 作为一种解决方法,您可以在存储过程中更改数据类型,例如,C程序是否可以在运行时将列“视为” varchar而不是smallint?

This is only a monthly process so the impact of doing the type conversion is not an issue. 这只是一个月的过程,因此进行类型转换的影响不是问题。

Yes, it can. 是的,它可以。 You can do so using either the CAST or CONVERT operator. 您可以使用CAST或CONVERT运算符来实现。 For instance: 例如:

CONVERT(varchar, MyIntColumn) AS MyIntColumn

That will ensure that when the column goes to the client, it goes as a varchar string. 这将确保当列转到客户端时,它作为varchar字符串发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM