sql表中插入数据,我要向A表中插入数据,其中sql只取一部分数据列的数据是来自B表另sql只取一部分数据列是固定值这时应该怎么写

sql&把a表的数据插入b表中,where某一个字段相同
两个表中&code一样&要把usp这个字段中的数据插到另一个表中没有数据的这个表中的usp字段
UPDATE test2,test3 SET test2.usp = test3.usp where
test2.code=test3.
sqlserver 和oracle:
update&ips&set&countryid=(select&countryid&from&country&where&ips.iso=country.iso&)
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&SqlServer触发器A表中插入一行数据后动态在B表中插入A表中计算后数据
SqlServer触发器A表中插入一行数据后动态在B表中插入A表中计算后数据
表t_mydata 的列定义如下:
int & id &,int & station_id, datetime data_time,float LJLL
示例数据:
& 1 & 8:00:00 &2039886
& 1 & 8:00:00 &2043351
表t_station 的列定义如下
Int & station_id,varchar station_name ,varchar &station_detail
1 & & MD1 & & 铭德二期
2 & & MD2 & & 铭德一期
表t_point 的列定义如下:
Int point_id,varchar point_name,varchar point_desc,int point_station_id
1 &MD1_LJLL &累计流量 & 1 &
2 &MD2_LJLL &累计流量 & 2
表t_DTLJ &的定义如下:
Datetime data_time, float MD1_DTLJLL,flaot MD2_DTLJLL
这三个表的关系是:
t_point的列point_name的内容是t_station表中station_name列内容加&_&加t_mydata表中数据列的列名。
t_mydata的station_id列 和 t_station表的 station_id列相连接。
t_DTLJ表的数据列的名称是t_station表的station_name列的内容加&_DT&加t_mydata表的LJLL名。
t_mydata表中列LJLL中的值是个累计增大的值。
我现在要求得是,向t_mydata表中插入一条数据后,计算出插入时间的数据和当天8点时的数据的差,将这差值插入到表t_DTLJ表中的对应的列中。
CREATE TRIGGER [CaculateDTLJ] ON [dbo].[t_mydata]&
FOR INSERT&
declare @time datetime & & & & ---插入时间
declare @stationid int & & & & & & &---站点ID
declare @station_name varchar(20) &---站点英文名称
declare @point_name varchar(20) & & ---点英文名称
declare @minTime datetime & & & & & --时间段内最小时间
declare @str varchar(20)
DECLARE @sql nvarchar(1000) & &--动态sql语句
declare @param nvarchar(100)
--将插入的数据的站点ID和插入时间赋值给变量。
select @stationid=station_id,@time=data_time from inserted
--声明游标 从t_point表中查询出所有累计量的点,累计量以LJ为关键字标志
并将累计量的名称,拆分为站名称和点名称
DECLARE name_cursor CURSOR
FOR &SELECT substring(point_name,0,CHARINDEX(&#39;_&#39;,point_name)) as &#39;station_name&#39;,substring(point_name,CHARINDEX(&#39;_&#39;,point_name)+1,len(point_name)-CHARINDEX(&#39;_&#39;,point_name)) as &#39;point_name&#39;
FROM t_point WHERE point_station_id= @stationid &and point_name like &#39;_%LJ%&#39;;
--打开游标
open name_cursor
--读取游标数据
FETCH NEXT FROM name_cursor into @station_name,@point_name
--循环判断游标中是否有数据未读取
WHILE @@FETCH_STATUS = 0
--时间段内最小值
declare @minval float
--时间段内最大值
declare @maxval float
--时间段内差值
declare @val float
set @str=@station_name+&#39;_DT&#39;+@point_name
--判断t_DTLJ表中是否含有列@str
IF COL_LENGTH(&#39;t_DTLJ&#39;, @str) IS NOT NULL &
--查询当天8点时的时间,如果当前时间大于8点,则当天8点时间为当天8点时间。如果当前时间小于8点,则当天8点时间为前一天8点时间。
select @minTime=min(data_time) &from t_mydata where station_id=@stationid and data_time between (CASE when datepart([hour], @time)=8 and datepart([mi], @time)=0 and &datepart([ss], @time)=0 &then DATEADD([hour],-16,convert(varchar(10),@time,120)) &when datepart([hour], @time)&=8 THEN DATEADD([hour], 8,convert(varchar(10),@time,120)) else DATEADD([hour],-16,convert(varchar(10),@time,120))end) and @time
--查询当天8点和当前时间的记录的差值
select @sql=&#39;select @minval=min(isnull(&#39;+@point_name+&#39;,-1)),@maxval=max(isnull(&#39;+@point_name+&#39;,-1)) from t_mydata where station_id=@stationid and &data_time in(@minTime,@time)&#39;
set @param = &#39;@time datetime,@minTime datetime,@stationid int,@minval float output,@maxval float output&#39;
print @sql
--执行sql语句
exec sp_executesql @sql,@param,@time,@minTime,@stationid,@minval out ,@maxval output
--滤掉空值
if @minval is null or @maxval is null
set @val=-1
if @minval !=-1 and &@maxval !=-1
set @val=@maxval-@minval
set @val=-1
select @val
declare @counts int
select @counts=count(*) from t_DTLJ &where data_time=@time
if @counts&0
--如果存在已有记录则更新记录,否则插入新纪录
select @sql=&#39;update t_DTLJ set &#39;+@str+&#39;=@val where data_time=@time&#39;
set @param=&#39;@val float,@time datetime&#39;
exec sp_executesql @sql,@param,@val,@time
select @sql=&#39;insert into t_DTLJ(data_time,&#39;+@str+&#39;) values(@time,@val)&#39;
set @param=&#39;@val float,@time datetime&#39;
exec sp_executesql @sql,@param,@val,@time
FETCH NEXT FROM name_cursor into @station_name,@point_name
--关闭游标
close &name_cursor
--删除游标
DEALLOCATE name_cursor
顶一下(0) 踩一下(0)
热门标签:sql当一个表中插入数据时候另一个表中对应的字段的数据也跟着插入_百度知道
sql当一个表中插入数据时候另一个表中对应的字段的数据也跟着插入
jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink">表a的字段 &nbsp://a.baidu://a.com/zhidao/pic/item/f2deb48f8c962ff5e0fe98257e42.hiphotos.baidu.jpg" esrc="/zhidao/wh%3D600%2C800/sign=15d9f8a40cf41bd5da06e0f261eaadf3/f2deb48f8c962ff5e0fe98257e42.hiphotos<a href="http
not find&#39在a表建个触发器就行了大概是这样create tgigger tr_ins_A ON Ainstead of insert ASif not exists(select * from inserted where Applicant not in (select Applicant from B))insert into Bselect * from insertedelseraiserror(&#39,16;
你的不能运行a
来自团队:
其他类似问题
为您推荐:
其他1条回答
建立一个触发器就好了.
不会创建啊这不求教啊
CREATE OR REPLACE TRIGGER trigger_nameAFTER INSERT ON tableaFOR EACH ROWBEGINinsert
into tableb select * from tablea where Applicant = :new.AEND;--前提两个表的字段一样,不同就要另外的一条insert插入
CREATE OR REPLACE TRIGGER tr_ActualTimeAFTER INSERT ON tbl_ActualTimeFOR EACH ROW消息 156,级别 15,状态 1,第 1 行关键字 &#39;OR&#39; 附近有语法错误。消息 102,级别 15,状态 1,第 5 行&#39;:&#39; 附近有语法错误。
你的不是oracle?我写的是oracle的!
不是啊 是SqlServer2008
我再跟你说明白点吧
就以我表A吧有字段Applicant
表B也有 根据表A字段Applicant与表B中中的Applicant对比 如果表B有就把表A的后面的ActualDay值给表B的字段ActualDay
等别人回答吧,我能写出来也是oracle的,数据库不同.
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁15:24 提问
SQLSERVER 表A某列数据插入B表
strong text
表tableA:
A B C D E F --列名
1 10 11 12
H I J k L M
我想把taleA 表中的BCD列数据提取出来 插入tableB 中 HIJ三列中形成新的一条数据。由于tableA中数据比较多,我想循环搜索和插入,请教如何实现。
按赞数排序
into tableB(H,I,J)
from tableA
where ....
我没理解错吧
2072关注|624收录
其他相似问题
相关参考资料

我要回帖

更多关于 sql 增加列 的文章

 

随机推荐