VB6 如何从结果集中插入 table 字段为空时的原样
VB6 How to insert into a table from resultset as it is when field is null
CREATE TABLE [dbo].[TABLE001]
(
[T001_int] [int] NULL,
[T001_char] [char](100) NULL,
[T001_decimal] [decimal](5, 0) NULL
)
CREATE TABLE [dbo].[TABLE002]
(
[T002_int] [int] NULL,
[T002_char] [char](100) NULL,
[T002_decimal] [decimal](5, 0) NULL
)
INSERT INTO TABLE002 VALUES (NULL, NULL, NULL)
TABLE 以上定义
Dim strQuery As String
Dim rdoEnv As rdoEnvironment
Dim rdoCon As rdoConnection
Dim rdoRS As rdoResultset
strQuery = ""
strQuery = strQuery & " SELECT * FROM TABLE002"
Set rdoEnv = rdoEnvironments(0)
Set rdoCon = rdoEnv.OpenConnection("", rdDriverNoPrompt, True, MyCon)
Set rdoRS = rdoCon.OpenResultset(strQuery, rdOpenKeyset, rdConcurReadOnly)
Do Until rdoRS.EOF = True
strQuery = ""
strQuery = strQuery & " INSERT INTO TABLE001"
strQuery = strQuery & " VALUES"
strQuery = strQuery & " (" & rdoRS!T002_int & ", '" & rdoRS!T002_char & "'," & rdoRST002_decimal & ")"
rdoCon.Execute (strQuery)
rdoRS.MoveNext
Loop
以上是我的VB6代码
当rdoRS!T002_int = null时,出现错误
当rdoRS!T002_char = null时,插入为''
当rdoRS!T002_decimal = null时,出现错误
我想插入 NULL 作为 NULL
我该如何解决这个问题
请不要告诉我不应该这样做。
我想知道如何插入为null。
你可以试试这个:
Dim int_value As String
Dim str_value As String
Dim dec_value As String
int_value = IIf(IsNull(myrs!T002_int), "NULL", CStr(myrs!T002_int))
str_value = IIf(IsNull(myrs!T002_char), "NULL", "'" & myrs!T002_char & "'")
dec_value = IIf(IsNull(myrs!T002_decimal), "NULL", CStr(myrs!T002_decimal))
strQuery = ""
strQuery = strQuery & " INSERT INTO TABLE001"
strQuery = strQuery & " (T001_int, T001_char, T001_decimal)"
strQuery = strQuery & " VALUES"
strQuery = strQuery & " (" & int_value & ", " & str_value & ", " & dec_value & ")"
一些注意事项:
- 插入时,始终指定列。
- 如果
T002_char
列包含引号或其他内容,您将收到错误消息
搞乱了 SQL 语法。
最好使用准备好的语句。
CREATE TABLE [dbo].[TABLE001]
(
[T001_int] [int] NULL,
[T001_char] [char](100) NULL,
[T001_decimal] [decimal](5, 0) NULL
)
CREATE TABLE [dbo].[TABLE002]
(
[T002_int] [int] NULL,
[T002_char] [char](100) NULL,
[T002_decimal] [decimal](5, 0) NULL
)
INSERT INTO TABLE002 VALUES (NULL, NULL, NULL)
TABLE 以上定义
Dim strQuery As String
Dim rdoEnv As rdoEnvironment
Dim rdoCon As rdoConnection
Dim rdoRS As rdoResultset
strQuery = ""
strQuery = strQuery & " SELECT * FROM TABLE002"
Set rdoEnv = rdoEnvironments(0)
Set rdoCon = rdoEnv.OpenConnection("", rdDriverNoPrompt, True, MyCon)
Set rdoRS = rdoCon.OpenResultset(strQuery, rdOpenKeyset, rdConcurReadOnly)
Do Until rdoRS.EOF = True
strQuery = ""
strQuery = strQuery & " INSERT INTO TABLE001"
strQuery = strQuery & " VALUES"
strQuery = strQuery & " (" & rdoRS!T002_int & ", '" & rdoRS!T002_char & "'," & rdoRST002_decimal & ")"
rdoCon.Execute (strQuery)
rdoRS.MoveNext
Loop
以上是我的VB6代码
当rdoRS!T002_int = null时,出现错误
当rdoRS!T002_char = null时,插入为''
当rdoRS!T002_decimal = null时,出现错误
我想插入 NULL 作为 NULL
我该如何解决这个问题
请不要告诉我不应该这样做。
我想知道如何插入为null。
你可以试试这个:
Dim int_value As String
Dim str_value As String
Dim dec_value As String
int_value = IIf(IsNull(myrs!T002_int), "NULL", CStr(myrs!T002_int))
str_value = IIf(IsNull(myrs!T002_char), "NULL", "'" & myrs!T002_char & "'")
dec_value = IIf(IsNull(myrs!T002_decimal), "NULL", CStr(myrs!T002_decimal))
strQuery = ""
strQuery = strQuery & " INSERT INTO TABLE001"
strQuery = strQuery & " (T001_int, T001_char, T001_decimal)"
strQuery = strQuery & " VALUES"
strQuery = strQuery & " (" & int_value & ", " & str_value & ", " & dec_value & ")"
一些注意事项:
- 插入时,始终指定列。
- 如果
T002_char
列包含引号或其他内容,您将收到错误消息 搞乱了 SQL 语法。
最好使用准备好的语句。