如何编写sql insert query + tb(变量)? [英] how to write sql insert query + tb(Variable)?

查看:103
本文介绍了如何编写sql insert query + tb(变量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hiii,



i有书面查询:===

hiii,

i have written query :===

INSERT INTO ContentSentences (ContentID,SentenceID,OrderNo) SELECT ContentID,SentenceID FROM Contents,Sentences WHERE ContentText LIKE '" + dt1.Rows[j]["SentenceText1"].ToString() + "' AND SentenceText LIKE '" + dt1.Rows[j]["SentenceText1"].ToString() + "',tb";





其中'+ dt1.Rows [j] [SentenceText1]。 ToString()+'是来自数据表的值,您可以将此值假设为某些文本。我想要t o将变量的值插入到名称为订单号的表中。



获取订单编号我已编写代码



in which '" + dt1.Rows[j]["SentenceText1"].ToString() + "'is value come from datatable you can assume this value as some text.and i want to insert variable's value into table that name is order number.

for getting ordernumber i have write the code

string tb = ((TextBox)GridView1.Rows[index].FindControl("txt1")).Text;





但我的查询无效。



but my query is not working.

推荐答案

首先,几个注释:

1)如果你想使用 SELECT 的记录集件。声明,您需要定义目录句子表之间的关系。

First of all, few notes:
1) If you want to use record set returnef by SELECT statement, you need to define relationship between Contents and Sentences tables.
SELECT c.ContentId, s.SentenceId
FROM Contents AS c INNER JOIN Sentences AS s ON c.PK = s.FK



其中 c.PK 表示内容的主键表和 s.FK 表示外键表单句子表格。换句话说,如果表之间没有关系,则笛卡尔结果集 [ ^ ]。(cROOS JOIN)。

如需了解更多信息,请参阅: SQL联接的可视化表示 [ ^ ]



2)输入和输出字段的数量必须相等!


where c.PK means Primary Key for Contents table and s.FK means Foreign Key form Sentences table. In other words, if there is no relationship between tables, the cartesian result set[^] is produced (CROOS JOIN).
For further information, please see: Visual Representation of SQL Joins[^]

2) The number of input and output fields must be equal!

INSERT INTO ContentSentences (ContentID,SentenceID,OrderNo)
SELECT ContentID, SentenceID, OrderNo
FROM ...





3 )数据类型必须是相同!

如果数据类型 ContentId SentenceID 是数字,那么值由 SELECT 语句返回的语句必须是相同的数据类型!



4)你无法以这种方式构建查询!

正确的方法是与paramaters一起使用命令。以这种方式构建查询:

SQL:



3) The data type must be the same!
If data type of ContentId and SentenceID is numeric, then the values returned by SELECT statement must be the same data type!

4) you can't build queries this way!
The proper way is to use command together with paramaters. Build the query this way:
SQL:

string qry = string.Concat("INSERT INTO ContentSentences (ContentID, SentenceID, OrderNo) ",
       "SELECT ContentID, SentenceID, ", tb , " AS OrderNo ",
       "FROM Contents ... Sentences ON ...",
       "WHERE ContentText LIKE '@Param1' AND SentenceText LIKE '@Param2'");





OleDb:



OleDb:

string qry = string.Concat("INSERT INTO ContentSentences (ContentID, SentenceID, OrderNo) ",
       "SELECT ContentID, SentenceID, ", tb , " AS OrderNo ",
       "FROM Contents ... Sentences ON ...",
       "WHERE ContentText LIKE '?' AND SentenceText LIKE '?'");





因为您没有提供有关数据库提供程序的足够信息,所以我无法向您显示示例代码。

SqlCommand.Parameters Property [ ^ ]

OleDbCommand.Parameters Property [ ^ ]


试试我的朋友:

Try this my friend:
INSERT INTO ContentSentences (ContentID,SentenceID,OrderNo) 
SELECT ContentID,SentenceID,"+tb+" FROM Contents,Sentences WHERE ContentText LIKE '" + dt1.Rows[j]["SentenceText1"].ToString() + "' AND SentenceText LIKE '" + dt1.Rows[j]["SentenceText1"].ToString() + "'";


这篇关于如何编写sql insert query + tb(变量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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