VB内用变量定义SQL内表名

来源:百度知道 编辑:UC知道 时间:2024/06/06 03:50:19
我想在VB中实现定义一个变量 然后赋值给变量一个字符串 这个变量的值就成为对应的SQL中的表名,然后在VB中使用SQL语句进行查询
sql = "select VAR(b02a_atp1_ia) as b02a_atp1_ia From dbo.B02A_ATP1 where [time] between ('" & s1 & "') and('" & s2 & "')"

我想在VB中将dbo.B02A_ATP1 和b02a_atp1_ia赋值给一个变量 在SQL语句中就用变量代替 查询条件我都是用S1和S2代替的可以实现 但是表名和列名我都无法实现 请高手帮忙一下 或者给点其他的方法。
sql = "select VAR(b02a_atp1_ia) as b02a_atp1_ia From dbo.B02A_ATP1 where [time] between ('" & s1 & "') and('" & s2 & "')"
是在VB中编译通过了的 功能也实现了 我现在想把dbo.B02A_ATP1 和b02a_atp1_ia也变成s1 s2的变量 但是编译就说是语法错误

用变量之后要用动态sql,即是 exec ( 你的sql语句才可以 )
sql = "select VAR(b02a_atp1_ia) as b02a_atp1_ia From dbo.B02A_ATP1 where [time] between ('" & s1 & "') and('" & s2 & "')"
sql = "exec(" & sql & ")"

a="b02a_atp1_ia"
b="dbo.B02A_ATP1"
sql = "select " & a & " as " & a & " From " & b & " where [time] between ('" & s1 & "') and('" & s2 & "')"

sql = "select VAR(b02a_atp1_ia) as b02a_atp1_ia From dbo.B02A_ATP1 where [time] between ('" & s1 & "') and('" & s2 & "')"
sql = "exec(" & sql & ")"