如何在表中插入字符串值

发布于 2024-12-19 10:29:19 字数 258 浏览 2 评论 0原文

使用 VB6 和 SQL Server

如何在表中插入字符串值

Dim a as string
a = 0008", "0009", "1011", "1208

我想在表中插入该字符串值

预期输出

Table1

ID

0008
0009
1011
1208

如何针对上述条件进行查询。

需要查询帮助。

Using VB6 and SQL Server

How to insert a string value in the table

Dim a as string
a = 0008", "0009", "1011", "1208

I want to insert this string values in the table

Expected Output

Table1

ID

0008
0009
1011
1208

How to make a query for the above condition.

Need Query Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋你朝朝暮暮 2024-12-26 10:29:19

请尝试以下方法:

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.

Please try with the below way:

Dim a as string
a = "0008,0009,1011,1208"
dim strArray as string() = Split(a, ",")

b
for each item as string in strArray

   query = "INSERT INTO  Table1(ID) values('"&item&"')"
   // Write Connection and Command code here
next

Update:

You can generate insert SQL statement for each item at a time and then write Connection and Command code, that would be more efficient.

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.
电影里的梦 2024-12-26 10:29:19
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

DECLARE @Str VARCHAR(100)
SET @Str = '0008", "0009", "1011", "1208'
SET @Str = REPLACE(@Str,'"','')
SELECT * FROM [dbo].Split(@Str) 

You can use Split function from this link.
Check this Split function.
http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/split-string-in-sql-server-2005-clr-vs-t

别忘他 2024-12-26 10:29:19

首先,您必须将此字符串拆分为字符串[]

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

at first u must split this string to a string[]

dim a as string 

a = 0008", "0009", "1011", "1208
dim str as string() = (a.Replace('"',' ')).Split(",")

and if

a = "0008,0009,1011,1208"
dim str as string() = a.Split(",")

now add values in the table with a query on the foreach loop

imaging your table name is testTable andyour column name is ID

 dim query as string
 for each item as string in str

            query = "INSERT INTO  testTable(ID) values('"&item&"')"
 next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文