sql - Loop for each row -
i have 2 tables foreign key([table_id])
columns id table_id activeflag 1 1 0 2 2 1 3 1 1 4 3 0 sys_tables table_id name 1 request 2 plan 3 contecst
i'm writing stored procedure returns column each table.
example output values above
--first output table id table_id activeflag 1 1 0 3 1 1 --second output table id table_id activeflag 2 2 1 --third output table id table_id activeflag 4 3 0
my idea this
select c.* ccolumns c inner join sys_tables t on t.table_id = c.table_id , t.table_id = @parameter
my problem, do't know how make loop each row. need best way. example can use following loop:
declare @i int = 0 declare @count int; select @count = count(t.table_id) sys_tables t while @i < @count begin set @i = @i + 1 --do above select end
but not entirely correct. example sys_tables such data may be
table_id name 1 request 102 plan 1001 contecst
do have idea?
there couple ways can achieve that: loops , cursors, first of need know it's bad idea: either slow, anyway, here's kind of loop sample:
declare @row_ids table ( id int identity (1, 1), rid int ); insert @row_ids (rid) select someidfield sometable declare @cnt int = @@rowcount declare @currentrow int = 1 while (@currentrow <= @cnt) begin select rid @row_ids id = @currentrow set @currentrow = @currentrow + 1 end
Comments
Post a Comment