PRB: Call to SQL Stored Procedure Returns 'Invalid Column Name'ID: Q222664
|
When calling a SQL Server stored procedure that uses Exec('string') syntax, you may get an error returned to the calling client if you are referencing values using double quotes. The error that is returned to the calling client is:
This error is being raised because the SQL Server driver is interpreting the value inside of the double quotes as a column name instead of a field value. The cause of this problem most likely is the SQL Server driver defaulting to setting the QUOTED_IDENTIFIER option to 'on'.-2147217900 -- [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid
column name 'Column'.
There are three ways to avoid this problem:
DROP PROC sptest
go
CREATE PROC sptest as
EXEC('SELECT au_id, au_lname, au_fname = (
CASE
when au_lname = "Ringer" then "value1"
else "default"
END
)
FROM Authors')
DROP PROC sptest
go
CREATE PROC sptest as
EXEC('SELECT au_id, au_lname, au_fname = (
CASE
when au_lname = ''Ringer'' then ''value1''
else ''default''
END
)
FROM Authors')
This behavior is by design.
DROP PROC sptest
go
CREATE PROC sptest as
EXEC('SELECT au_id, au_lname, au_fname = (
CASE
when au_lname = "Ringer" then "value1"
else "default"
END
)
FROM Authors')
Private Sub Command1_Click()
Const CONNECTSTR = "Provider=MSDASQL.1;Persist Security Info=False;" _
& "Extended Properties=Driver={SQL Server}; " _
& "SERVER=(local);UID=sa;DATABASE=pubs"
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
With cn
.ConnectionString = CONNECTSTR
.CursorLocation = adUseServer
.Open
End With
' Uncomment this line to avoid error.
' cn.Execute "SET QUOTED_IDENTIFIER OFF"
Set cmd = New ADODB.Command
With cmd
.CommandText = "sptest"
.CommandType = adCmdStoredProc
.ActiveConnection = cn
End With
Set rs = cmd.Execute
Do Until rs.EOF
sz = ""
For i = 0 To rs.Fields.Count - 1
sz = sz & rs(i).Value & " | "
Next i
Debug.Print sz
rs.MoveNext
Loop
End Sub
Microsoft SQL Server Books Online
For more information, please see the following article(s) in the Microsoft Knowledge Base:
Q156501 INF: QUOTED_IDENTIFIER and Strings with Single Quotation Marks
Q135531 PRB: Double Quotation Mark Params Error w/ Quoted Identifiers
Additional query words: kbdse
Keywords : kbADO kbDatabase kbMDAC kbSQLServ kbStoredProc kbGrpVBDB
Version : WINDOWS:1.5,2.0,2.1
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: April 15, 1999