奇特的用法:把列名取到一个字符串中

发表于:2007-06-30来源:作者:点击数: 标签:
if exists(select * from sysobjects where type=‘’P‘’ and name=‘’sys_ysl_getColumnNameToArrayByTableName‘’ ) drop procedure sys_ysl_getColumnNameToArrayByTableName go create procedure sys_ysl_getColumnNameToArrayByTableName @tablename
if exists(select * from sysobjects where type=‘’P‘’ and name=‘’sys_ysl_getColumnNameToArrayByTableName‘’ )
drop procedure sys_ysl_getColumnNameToArrayByTableName
go
create procedure sys_ysl_getColumnNameToArrayByTableName
@tablename varchar(100),
@colnameArrayList varchar(1000) output

With
Encryption
as
declare @sql nvarchar(1000)
declare @colnameArray varchar(1000)
declare @colname varchar(50)
SET @sql =N‘’DECLARE CUR_COLNAME CURSOR FOR select a.name from syscolumns a inner join sysobjects b on a.id=b.id where b.name=‘’‘’‘’+@tablename+N‘’‘’‘’ order by a.colorder ‘’

execute sp_executesql @sql

OPEN CUR_COLNAME
WHILE (0=0)
BEGIN
FETCH NEXT FROM CUR_COLNAME
INTO @colname

If (@@fetch_status<>0) break

IF @colnameArray<>‘’‘’
set @colnameArray=@colnameArray+‘’,‘’+@colname
Else
set @colnameArray=@colname

END

Close CUR_COLNAME
DEALLOCATE CUR_COLNAME

set @colnameArrayList=@colnameArray


/*
print @colnameArrayList -- 供测试使用

--测试代码
DECLARE @@AA VARCHAR(100)
exec sys_ysl_getColumnNameToArrayByTableName ‘’SPBLA‘’,@@AA
--结束

sp_helptext sys_ysl_getColumnNameToArrayByTableName --查看过程文本
*/



原文转自:http://www.ltesting.net