sql中 in 陳述句 如果是字串查詢出錯,
m2:='p001,p002'
select * from kucuntx where ypbh in ('+m2+')'
Server: Msg 207, Level 16, State 3, Line 3
Invalid column name 'p002'.
Server: Msg 207, Level 16, State 1, Line 3
Invalid column name 'p001'.
但如果是:m2:='111,222' 查詢沒問題
uj5u.com熱心網友回復:
m2:='''p001'',''p002'''uj5u.com熱心網友回復:
我得到的m2的格式是 'p001,p002' 要轉換成 in陳述句識別的格式:'p001','p002'uj5u.com熱心網友回復:
如果m2是 數字型的,比如:‘100,200’ ,在In陳述句就沒問題,in ('+m2+'),如果是字符型的,使用in ('+m2+')就有問題,要把‘p100,p200’ 轉換成 'p100','p200' 格式 In陳述句才識別
uj5u.com熱心網友回復:
可以在SQL Server中執行一下下面的陳述句。with table1 as
(
select 1 id union all
select 3 id union all
select 5 id union all
select 7 id union all
select 9 id union all
select 11 id union all
select 13 id union all
select 15 id union all
select 17 id union all
select 19 id
)
select * from table1 where id in ('1','2','3')
測驗說明字串可以自動轉換成數字。
因此,無論m2得到什么樣的內容,都轉換成文字。例子如下:
如果m2:='p001,p002',就轉換成m2:='‘’p001‘’,‘’p002‘’'
如果m2:='100,200',就轉換成m2:='‘’100‘’,‘’200‘’'
這樣,無論是字符或數字,in ('+m2+')就不會有錯誤了。
uj5u.com熱心網友回復:
delphi中sql字串要用雙引號uj5u.com熱心網友回復:
建議這么寫,比較好讀:'select * from kucuntx where ypbh in ('+#39+m2+#39+')'
或者引數方式也不錯
uj5u.com熱心網友回復:
SQL 中IN是一個集合,對于整型:in (1,2,3,4,5,6)
對于字符型:in ('1','2','3','4','5','6')
m2:='p001,p002'//這個運算式應該是:m2:='p001','p002'
select * from kucuntx where ypbh in '('+m2+')'//這樣就沒有問題了
uj5u.com熱心網友回復:
樓主不懂字串是要加單引號的么, 直接是數字可以成功那只是SQL的施舍,最好還是對自己嚴格要求一點.轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/78844.html
標籤:數據庫相關
上一篇:如何復位XE8的開發環境
