2013-03-21 142 views
1

您好我正在使用select语句将值插入到表中。但插入不会发生。 select语句的结果集中有5行。但是当我激发插入语句与选择数据没有被插入到表中。使用select语句在表中插入

我检查了属性的顺序,一切似乎都是正确的。我还尝试使用“values”关键字在表格中插入单行,并在此帮助下插入数据。 为什么我的插入不能使用“select”命令?

请帮忙。

下面是查询

insert into schema1.tabletemp 
    select distinct 
    a.name as name, 
    a.stu_number as rollno, 
    c.userid as login_id, 
    b.Address as stuAddress, 
    b.totalgrades as FinalGrdaes 
    ' ' as Misc 
from 
    Schema1.stu_info a, schema1.address_info b, schema1.logindetails c 
where 
    a.stu_no = b.record_no and 
    a.status in ('Active') and 
    c.last_name=a.stu_lname and 
    c.first_name=a.stu_fname and 
    a.stu_no not in (select distinct student_number from schema1.student_final_records); 

这个查询试图学生数据插入到tabletemp。标准是student_final_record表中存在的学生记录不应再次插入。因此,系统中新增的任何学号,即不存在于student_final_record中的学号都将插入到tabletemp中。

不在工作中不工作。有什么建议么????

+3

请shjare您的DDL,样本数据,并插入查询 – ljh 2013-03-21 21:27:38

+2

向我们展示你的SQL,也许我们可以帮助。 – 2013-03-21 21:29:09

+1

您是否在INSERT ... SELECT之后执行了COMMIT语句? – 2013-03-22 02:35:29

回答

0

有一个逗号缺少在这里:

b.totalgrades as FinalGrdaes 
    ' ' as Misc 

固定的版本:

b.totalgrades as FinalGrdaes, 
    ' ' as Misc 
+0

谢谢我纠正它。我使用提交插入语句,但现在它一直插入相同的行。我在查询中检查了“NOT IN”子句的用法,它不起作用。每次我发射这个查询时,它都返回相同的行。从schema1.tabletemp中选择a.stu_no a 其中a.stu_no不在(从schema2.student_final_record中选择student_number); – user2171679 2013-03-25 14:26:57

+0

谢谢我纠正它。我使用提交插入语句,但现在它一直插入相同的行。我在查询中检查了“NOT IN”子句的用法,它不起作用。每次我发射这个查询时,它都返回相同的行。从schema1.tabletemp中选择a.stu_no a 其中a.stu_no不在(从schema2.student_final_record中选择student_number);我不确定这个查询是否正确。请看看并让我知道 – user2171679 2013-03-25 14:32:35

+0

您的NOT IN子句解决了查询中未包含的表,因此您所做的任何操作都不会更改此查询返回的值。 – 2013-03-25 14:44:26