使用VB6和SQL Server
如何在表格中插入字符串值
Dim a as string
a = 0008", "0009", "1011", "1208
我想在表
中插入此字符串值预期产出
表1
ID
0008
0009
1011
1208
如何查询上述情况。
需要查询帮助。
答案 0 :(得分:1)
请尝试以下方式:
Dim a as string
a = "0008,0009,1011,1208"
dim strArray as string() = Split(a, ",")
b 对于strArray中的每个项目作为字符串
query = "INSERT INTO Table1(ID) values('"&item&"')"
// Write Connection and Command code here
next
<强>更新强>
您可以一次为每个项生成插入SQL语句,然后编写更高效的连接和命令代码。
dim query as string
for each item as string in strArray
query = query + "INSERT INTO Table1(ID) values('"&item&"'); "
next
// Write Connection and Command code here after generating insert statement
// so that all data will save into database in one shot.
答案 1 :(得分:0)
DECLARE @Str VARCHAR(100)
SET @Str = '0008", "0009", "1011", "1208'
SET @Str = REPLACE(@Str,'"','')
SELECT * FROM [dbo].Split(@Str)
您可以使用此链接中的分割功能 检查此拆分功能 http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/split-string-in-sql-server-2005-clr-vs-t
答案 2 :(得分:0)
首先,您必须将此字符串拆分为字符串[]
dim a as string
a = 0008", "0009", "1011", "1208
dim str as string() = (a.Replace('"',' ')).Split(",")
如果
a = "0008,0009,1011,1208"
dim str as string() = a.Split(",")
现在使用foreach循环上的查询在表中添加值
为您的表格成像 testTable ,您的列名称为 ID
dim query as string
for each item as string in str
query = "INSERT INTO testTable(ID) values('"&item&"')"
next