判斷多個表是否加了預設值指令碼

2021-05-23 12:28:59 字數 1779 閱讀 5647

set nocount on

create table #tb(id int identity(1,1), tbname varchar(100))

insert into #tb(tbname)

select 'tbcustuse_gold'

union all select 'tbcustuse_pj'

union all select 'tbcustuse_pj'

union all select 'tborderbalance_report_dtl'

union all select 'tborderbalance_report_gold'

union all select 'tborderbalance_report_pj'

union all select 'tborderbalance_report_stone'

union all select 'tborderbalance_report_sum'

union all select 'tborderbalance_stoneprint'

union all select 'tborderbalance_stoneprint_cust'

declare @maxid int,@i int

select @maxid=max(id) from #tb

set @i=1

while @i<=@maxid

begin

declare @cur cursor,@name varchar(100),@type varchar(50)

declare @table varchar(100),@s varchar(2000)

select @table=tbname from #tb where id=@i

set @cur=cursor for select a.name,b.name from syscolumns a inner join systypes b on a.xtype=b.xtype where a.id=object_id(@table)

open @cur

fetch next from @cur into @name,@type

while @@fetch_status=0

begin

set @s=''

set @s='if not exists(select d.name from syscolumns a join sysobjects b on a.id=b.id

join syscomments c on a.cdefault=c.id join sysobjects d on c.id=d.id

where b.name= '''+@table+''' and a.name='''+@name+''')

alter table '+@table+' add constraint df_'+@table+'_'+@name+' default ('+

case when charindex('int',@type)>0 or @type='numeric' then '0'

when @type='datetime' then 'getdate()' else '''''' end +') for '+ @name+ char(10)+

'go'

print @s

fetch next from @cur into @name,@type

endclose @cur

deallocate @cur

set @i=@i+1

enddrop table #tb

修改表的預設值

乙個表中所有字段繫結約束的值 select name from sysobjects where object id initialization parent obj and xtype d 修改表中某乙個欄位的預設值 1 首先找出該欄位繫結約束的值 set tablename 表名 set fi...

預設值表 C 參考

visual studio 2013 其他版本 本主題尚未接受評分 為這個主題評分 下表顯示了預設建構函式所傳回的實值型別預設值。使用 new 運運算元時會叫用預設建構函式,如下所示 int myint new int 上述陳述式與下列陳述式有相同的效果 int myint 0 記住 c 裡不允許使...

預設值表(C 參考)

下表顯示了由預設建構函式返回的值型別的預設值。預設建構函式是通過new運算子來呼叫的,如下所示 int myint new int 以上語句同下列語句效果相同 int myint 0 請記住 在 c 中不允許使用未初始化的變數。值型別預設值 bool false byte 0 char 0 deci...