请教insert into语句
admin 发表于 2010-04-21 | 来源:互联网 | 阅读:
我现在只知道Session["username"],但是我要插入的数据是与之对应的id(username和id在同一张表users)到另一个表examine中,请问我的insert into 语句要怎么写啊??
insert into examine (ex_u_id) values (select u_id from users where u_username=
‘" +Session["username"].ToString()+ "’)???有没有这种写法啊?

insert into examine(ex_u_id) values (select u_id from users where u_username=’" +Session["username"].ToString()+ "’)可以。没发现问题。建议,为了更好的代码,建议参数化,而不是用+这样来拼接字符串
sql 语法有问题应该是insert into tableName () select () from tableName()里面是要填充的字段
string sql=string.Format("insert into examine ([ex_u_id]) select u_id from users where u_username=’{0}’ ",Session["username"]);大概意思,具体细节自己修改
阿非 真厉害。我也没注意,语法错误了。语法是:insert into destTbl(fld1, fld2) select fld1, 5 from srcTbl所以应该是:insert into examine(ex_u_id) select u_id from users where u_username=’" +Session["username"].ToString()+ "’;
4楼说的这个语法我知道,问题是我插入的字段不止一个,其他的都可以用values,insert into examine ex_u_id,ex_applycontent,ex_define values (select u_id from users where u_username=’" +Session["username"].ToString()+ "’,’" + exreason + "’,'" + exdetail + "’),如果语法没错的话,为什么我添加怎么都是失败!!!
额, 每个WHERE的限制条件, 要用 AND 连接,
sql 语法有问题
楼上的大侠,不要只跟我说错误嘛,有什么方法可以补救?insert into examine (ex_u_id) select u_id from users where u_username=’" + Session["username"].ToString() + "’ and (ex_applycontent,ex_define) values (‘" + exreason + "’,'" + exdetail + "’)换成这个还有语法错误吗?
你的语法错误了。分开,清晰点。应该是:C#代码。string username=Session["username"].ToString();string sql="insert into examine(ex_u_id,ex_applycontent,ex_define) select u_id,’{0}’,'{1}’ from users where u_username=’{3}’";sql=string.Format(sql,exreason,exdetail, username);直接写SQL语句的话,应该是:insert into examine(ex_u_id,ex_applycontent,ex_define) select u_id,’内容1′,’内容2′ from users where u_username=’名称’
你不看回帖的?
感谢♂风车车.Net (用户名:xray2005),我的问题解决了!!