| Author |
Topic |
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-13 : 05:39:36
|
i just want to add a column to all tables in DB using cursor..how we can do that????? with regards,Divya |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-13 : 05:41:39
|
Write code?sp_msforeachtable 'ALTER ? ADD x INT' E 12°55'05.63"N 56°04'39.26" |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-13 : 05:58:47
|
| could you plz help me with code.....with regards,Divya |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-13 : 06:00:18
|
| till now i have done like this .... because i dont want to enter each every table names in a dbdeclare @tblname varchar(1000)declare cur1 cursor iter forselect name from sys.tablesopen cur1fetch next from cur1 into@tblwhile @@fetch_status=0beginalter table dbo.'+@tbl+'add column 'employee id varchar(50)'endwith regards,Divya |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-13 : 06:08:25
|
quote: Originally posted by divyagr till now i have done like this .... because i dont want to enter each every table names in a dbdeclare @tblname varchar(1000)declare cur1 cursor iter forselect name from sys.tablesopen cur1fetch next from cur1 into@tblwhile @@fetch_status=0beginalter table dbo.'+@tbl+'add column 'employee id varchar(50)'endwith regards,Divya
Peso's code would do what you wantDid you try?MadhivananFailing to plan is Planning to fail |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-13 : 06:11:35
|
i have tried but it shows error ... as follows Msg 155, Level 15, State 2, Line 3'iter' is not a recognized CURSOR option.Msg 102, Level 15, State 1, Line 6Incorrect syntax near 'cur1'.Msg 102, Level 15, State 1, Line 9Incorrect syntax near '+@tbl+'.with regards,Divya |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2009-01-13 : 06:13:56
|
| I assume this is done manually and personally I'd prefer something like this instead:SELECT 'ALTER TABLE ' + table_name + ' ADD myColumn INT'FROM information_schema.tablesWHERE table_type = 'BASE TABLE'I think it gives me better control of what's actually going on even though there is really not much difference.- Lumbago |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-13 : 06:14:35
|
| i dont know how to do with peso's code... could you help me plzzzzwith regards,Divya |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2009-01-13 : 06:17:21
|
If you want to do it all in one go without copy/paste the generated code you could do like this:DECLARE @sql nvarchar(max)SET @sql = ''SELECT @sql = @sql + 'ALTER TABLE ' + table_name + ' ADD myColumn INT 'FROM information_schema.tablesWHERE table_type = 'BASE TABLE'PRINT @sqlEXEC sp_executesql @sql - Lumbago |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-13 : 06:20:23
|
| hi lumbago.... thank its working.... really i m thankful to youwith regards,Divya |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2009-01-13 : 06:21:17
|
| Excellent :)- Lumbago |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-13 : 23:23:32
|
| hi ..thank u visakh (for link you have given)with regards,Divya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-13 : 23:43:34
|
| welcome |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-14 : 02:09:59
|
| hi all, i have tried this code but this one is also shows error use workoutssp_MSforeachtable @command1='altertable''?'' add news int'error is as followsMsg 102, Level 15, State 1, Line 1Incorrect syntax near 'news'.with regards,Divya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-14 : 02:15:45
|
| it should besp_MSforeachtable @command1='altertable ? add news int' |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-14 : 02:17:28
|
| hi visakh, it shows error like disMsg 102, Level 15, State 1, Line 1Incorrect syntax near '.'.with regards,Divya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-14 : 02:19:24
|
| oh...you missed a space toosp_MSforeachtable @command1='alter table ? add news int' |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-14 : 02:21:38
|
| hi visakh , THANK uuuuuuuuuuuuuuuuuuuuuuu ;)with regards,Divya |
 |
|
|
divyagr
Starting Member
29 Posts |
Posted - 2009-01-14 : 02:26:08
|
| HI VISAKH ... ITS WORKING FINE.....with regards,Divya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-14 : 02:50:15
|
welcome |
 |
|
|
Next Page
|