tsql 中的字符串填充
我打印出一堆动态创建的 DDL 语句,并希望以特定方式对齐输出。
PRINT 'ALTER TABLE ' + @TableName + ' WITH NOCHECK ADD CONSTRAINT CK_' + @TableName + '_' + @ColumnName + '_MinimumLength CHECK (LEN(' + @ColumnName + ') > 0)'
输出:
ALTER TABLE SignType ADD CONSTRAINT CK_SignType_Description_MinimumLength CHECK (LEN(Description) > 0)
ALTER TABLE Person ADD CONSTRAINT CK_Person_Name_MinimumLength CHECK (LEN(Name) > 0)
我想要的输出是:
ALTER TABLE SignType WITH NOCHECK ADD CONSTRAINT CK_SignType_Description_MinimumLength CHECK (LEN(Description) > 0)
ALTER TABLE Person WITH NOCHECK ADD CONSTRAINT CK_Person_Name_MinimumLength CHECK (LEN(Name) > 0)
是否有一个函数允许我用字符 x 的 n 填充字符串。我会这样使用它:
PRINT 'ALTER TABLE ' + @TableName + PAD(' ', 50 - LEN(@TableName)) + ' WITH NOCHECK ADD CONSTRAINT .....'
谢谢
I print out a bunch of DDL statements that are dynamically created and want to align the output in a specific way.
PRINT 'ALTER TABLE ' + @TableName + ' WITH NOCHECK ADD CONSTRAINT CK_' + @TableName + '_' + @ColumnName + '_MinimumLength CHECK (LEN(' + @ColumnName + ') > 0)'
Output:
ALTER TABLE SignType ADD CONSTRAINT CK_SignType_Description_MinimumLength CHECK (LEN(Description) > 0)
ALTER TABLE Person ADD CONSTRAINT CK_Person_Name_MinimumLength CHECK (LEN(Name) > 0)
What I want the output to be:
ALTER TABLE SignType WITH NOCHECK ADD CONSTRAINT CK_SignType_Description_MinimumLength CHECK (LEN(Description) > 0)
ALTER TABLE Person WITH NOCHECK ADD CONSTRAINT CK_Person_Name_MinimumLength CHECK (LEN(Name) > 0)
Is there a function that allows me to pad the string by n of character x. I would use it like this:
PRINT 'ALTER TABLE ' + @TableName + PAD(' ', 50 - LEN(@TableName)) + ' WITH NOCHECK ADD CONSTRAINT .....'
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您想要的是 SQL 2005 中提供的
REPLICATE
函数。MSDN:复制
I believe what you want is the
REPLICATE
function available from SQL 2005 on.MSDN: REPLICATE
您可以使用 SPACE 函数:
如果您要插入的不是空格,则可以使用 REPLICATE(varchar, int)。
You can use the SPACE function:
If it's something else than a space you want to insert, you can use REPLICATE(varchar, int).
您需要使用 REPLICATE 函数,与数据长度一起:
You need to use the REPLICATE function, along with DATALENGTH: