如何将最后50条记录从现有表中插入到其他表中 [英] how to Insert last 50 Records into other table from existing table

查看:72
本文介绍了如何将最后50条记录从现有表中插入到其他表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello FRIENDS,



我正在使用asp.net,c#,SqlServer 2005.



我的要求是。,我有一个数据库通过Gridview页脚插入记录。

它的工作正常。



我有两张桌子SqlServer数据库......好吧

1)RecordsTable

2)ArchievedTable



所以,当用户输入记录时来自Gridview Footer。如果记录达到200条记录。最后的50条记录必须自动插入到ArchievedTable。



请你能帮助我。



谢谢。

Hello FRIENDS,

am working on asp.net , c# , SqlServer 2005.

my requirement is., I have a database to insert the records through Gridview Footer.
Its works fine.

I have Two tables in SqlServer Database...ok
1)RecordsTable
2)ArchievedTable

so, when user enters records from Gridview Footer. if the records reachs 200 records. Last old 50 Records must be automatically inserted into ArchievedTable.

Please can you help me.

Thanks.

推荐答案

使用存储过程/触发器(在某些程度上运行你的马)你可能会使用这些代码,例如



Use a stored proc/trigger(run the horses of u r mind somewhat) for this some code you may use like

declare @rec int
select @rec=count(*) from RecordsTable

if(@rec>200)
begin

select top 50 * from RecordsTable --declare a table variable for this
--Now use a loop / MERGE command(probably N/A in SQL 2005) to sync the tables
--using insert query

end







试试你可以完成它!!



祝你好运




Try u can get it done!!

Best of luck


如果有像id这样的字段,那就是顺序增加....或者像1,2,3,4这样的表中的任何autoid字段......然后做以下.....

1.插入编码检查行数

2.如果行数为200.

3.Execute命令一是将记录从一个表转移到secound,由id按顺序选择asc

4.从第一个表中删除

5.Refresh gridview ... rebind到数据库...



exa .....

if there any field like id....which is sequentially increasing....or any autoid field in table like1,2,3,4... then do following.....
1.On insert coding check row count
2.If row count is 200.
3.Execute to command one is to transfer record from one table to secound which is selected by order by id asc
4.Delete from first table
5.Refresh gridview...rebind to database...

exa.....
//your insert coding is here




int RecordsCount = Gridview1.Rows.Count ;

if (RecordsCount == 200)
{
//execute folowing command
cmd=new SqlCommand("insert into ArchievedTable(col_a, col_b)
select top 50 col1, col2 from RecordsTable
order by id asc",con);
cmd1=new SqlCommand("DELETE FROM RecordsTable WHERE id IN (SELECT TOP 50 id FROM RecordsTable ORDER BY id asc)",con);
cmd.ExecuteScaler();
cmd1.ExecuteScaler();

//then call gridbinding code here i.e refresh grid
BindDataToGrid();
}





这样的东西....希望它会有所帮助....



something like this....hope it will help....

for (int i = 0; i < (GridView1.Rows.Count) - 50; i++)
       {
           // save routine for Table that contains first 150 records
           BindWebControls.SetDataTarget(cn, "insert into put values('" + GridView1.Rows[i].Cells[0].Text + "')", 1, Label1);
       }

       for ( int z =(GridView1.Rows.Count) - 50;  z < GridView1.Rows.Count; z++)
       {
           // save routine for Table that contains last 50 records
           BindWebControls.SetDataTarget(cn, "insert into put1 values('" + GridView1.Rows[z].Cells[0].Text + "')", 1, Label1);
       }





如果有帮助你回复



reply if it helped you


这篇关于如何将最后50条记录从现有表中插入到其他表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆