SQL Server "Lazy" can also update data tables

Source: Internet
Author: User

Q: Please expert advice! The problem is this: I have 1000 data tables, each with the same structure (each table has "QQ,TJ,YJ,EJ,SJ,SIJ,WJ,LJ,ZS,ZJL" 10 fields), but the table name is different. There is also a "Data Update table JJ (TABLE_INDEX,QQ,TJ,YJ,EJ,SJ,SIJ,WJ,LJ,ZS,ZJL)", in addition to the Table_index field, also has "Qq,tj,yj,ej,sj,sij,wj,lj,zs, Zjl "10 fields, 1000 rows, the purpose of which is to update (insert) The preceding 1000 data tables, that is, each row of data update a table. But if manual updates are cumbersome, and the contents of the "Data Update table" change frequently, in other words, to constantly update the 1000 data tables mentioned above, I would like to be able to write a program to make the data automatically updated, so I first put the table names of these 1000 tables in the table Table_index (ordinal number, datasheet name) , and then write the following function (in which the return value is not actually used). But the grammar check "@table_name" is wrong, do not know how to correct, or whether this method is feasible? If it is not feasible, would the expert please give me some pointers on the "lazy" method for updating the 1000 data sheets? I will be extremely grateful!

  use fff
go
create function data_insert1()
RETURNS int
Begin
 Declare @qq int(4)
 declare @table_index int(4),@tj int(4),@yj int(4),@ej int(4),@sj int(4),@sij bigint(8)
 declare @wj bigint(8),@lj bigint(8),@zs bigint(8),@zjl char(15),@table_name char(15)
 declare youbiao cursor for select * from jj for read only
 open youbiao
 fetch next from youbiao into @table_index,@qq,@tj,@yj,@ej,@sj,@sij,@wj,@lj,@zs,@zjl
 while @@fetch_status=0 /*loop rows in the cursor*/
 begin
  declare youbiao_temp cursor for
  select 数据表名 from table_index where 序号=@table_index
  open youbiao_temp
  fetch next from youbiao_temp into @table_name 
  insert @table_name(qq,tj,yj,ej,sj,sij,wj,lj,zs,zjl)
  values(@qihao,@tj,@yj,@ej,@sj,@sij,@wj,@lj,@zs,@zjl)
  deallocate youbiao_temp
 end
 deallocate youbiao
 RETURN @table_index
end
go

A: Sure, this method works. The mistake is not understood. field names, table names, database names, and so on as variables, you must use dynamic SQL to resolve the problem using dynamic SQL statements.

use FFF
Go
Create function data_insert1 ()
RETURNS int
Begin
Declare @qq Int (4)
Declare @ Table_index Int (4), @tj Int (4), @yj Int (4), @ej Int (4), @sj Int (4), @sij bigint (8)
Declare @wj bigint (8), @lj bigint (8), @ ZS bigint (8), @zjl Char, @table_name char
Declare youbiao cursor for select database table name, Qq,tj,yj,ej,sj,sij,wj,lj,zs , Zjl from JJ A,table_index b where a.table_index=b. Ordinal for Read only
Open Youbiao
Fetch next from Youbiao into @tab  Le_name, @qq, @tj, @yj, @ej, @sj, @sij, @wj, @lj, @zs, @zjl
while @ @fetch_status =0/*loop rows in the cursor*/
begin
DECLARE @s Nvarchar (1000)
Set @s = ' Insert ' + @table_name + ' (QQ,TJ,YJ,EJ,SJ,SIJ,WJ,LJ,ZS,ZJL) ' Values ' + ' (' + @qq + ', ' + @tj + ', ' + @yj + ', ' + @ej + ', ' + @sj + ', ' + @sij + ', ' + @wj + ', ' + @lj + ', ' + @zs + ', ' + @zjl + ') '
exec (@s)
exec sp_executesql @s Open Youbiao
Fetch next from Youbiao to @table_name, @qq, @tj, @yj, @ej, @sj, @sij, @wj, @lj, @zs, @zjl
End
Deall Ocate Youbiao
Return 1
End
Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.