查找 TSQL 中的保留关键字
我不确定sql server 2008中是否有任何内置函数可以判断它是否是保留关键字。
我想这样做的原因是因为我发现有时列名使用与保留关键字相同的名称,例如,名为“desc”、“user”、“state”等的列,然后我们必须将其将它们用方括号([desc]、[user]、[state])括起来,以便能够正确查询列。
如果这样的内置函数确实存在,那么我们可能可以这样做
if isReservedKeyword (@name) = true
set @column = REPLACE(@column, @name, '[' + @name+ ']')
else
set @column = @name
I am not sure if there is any built-in function in sql server 2008 that will tell whether it is reserved keyword or not.
The reason I wanted to do this is because I find sometimes the column names are using the same name as the reserved keywords, for example, a column called 'desc', 'user', 'state', etc, which then we have to wrap them with square brackets ([desc], [user], [state]) to be able to query the columns correctly.
If such a built-in function does exist, then we probably can do
if isReservedKeyword (@name) = true
set @column = REPLACE(@column, @name, '[' + @name+ ']')
else
set @column = @name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保留字记录在此处:
http://msdn.microsoft.com/en-us/library/ms189822.aspx
该列表很详尽,但并没有太长,以至于您无法将它们重新输入到自己的数据库表中进行检查。
Reserved words are documented here:
http://msdn.microsoft.com/en-us/library/ms189822.aspx
That list is exhaustive, but it's not so long that you couldn't just re-enter those into your own database table to check against.
有一个内置函数可以处理这个问题,还有“不寻常”的字符:
QUOTENAME
:
There is a built in function that will take care of this, and also 'unusual' characters:
QUOTENAME
:
只需在每一列周围加上括号即可。这样您就可以确保保留字也得到处理。
Just put brackets around every column. That way you ensure that even reserved words are taken care of.