1 ALTER FUNCTION [dbo].[GenCode] 2 ( 3 @TbName NVARCHAR(200),@strFormat NVARCHAR(200) 4 ) 5 RETURNS @Table TABLE (Code nvarchar(4000)) 6 as 7 BEGIN 8 DECLARE @className NVARCHAR(200) 9 10 IF(@TbName LIKE '%_%') BEGIN 11 SET @className=RIGHT(@TbName,LEN(@TbName)-CHARINDEX('_',@TbName)) 12 END 13 INSERT INTO @Table ( Code ) 14 SELECT REPLACE(REPLACE(REPLACE(@strFormat,'{T}',@className),'{C}',c.name),'{N}',CAST( ISNULL(e.[value],'xxxx') AS NVARCHAR(200))) 15 FROM sys.objects o 16 INNER JOIN sys.columns c ON o.object_id=c.object_id and o.Type='U' 17 LEFT JOIN sys.extended_properties e on e.major_id=c.object_id and e.minor_id=c.Column_id and e.class=1 18 WHERE o.type='U' AND o.name=@TbName 19 20 RETURN 21 END
接触一个项目,有几个表都是有好几十个字段,做数据绑定时太烦神了,花个几分钟写了个函数,批量生成下