Showing posts with label sproc. Show all posts
Showing posts with label sproc. Show all posts

Wednesday, March 28, 2012

Invalid Syntax in my sproc?!?!

Hi I have a gridview that is being populated from a method that gets it's data from a table view.

SELECT dbo.cis_AlumniContact.Street, dbo.cis_AlumniContact.City, dbo.cis_AlumniContact.State, dbo.cis_AlumniContact.Telephone,
dbo.cis_AlumniContact.Occupation, dbo.cis_AlumniContact.Description, dbo.cis_AlumniContact.Zip, dbo.cis_AlumniContact.FirstName,
dbo.cis_AlumniContact.LastName, dbo.cis_AlumniContact.YearGraduate, dbo.cis_AlumniContact.Email, dbo.cis_AlumniContact.Contact,
dbo.aspnet_Users.UserName, dbo.cis_StudentId.UaaStudentId
FROM dbo.aspnet_Users INNER JOIN
dbo.cis_AlumniContact ON dbo.aspnet_Users.UserId = dbo.cis_AlumniContact.UserId INNER JOIN
dbo.cis_StudentId ON dbo.aspnet_Users.UserId = dbo.cis_StudentId.UserId

No big deal, works great. Now when I click update I call this method

<DataObjectMethod(DataObjectMethodType.Update)>

PublicSub UpdateAlumni(ByVal StreetAsString,ByVal CityAsString,ByVal StateAsString,ByVal TelephoneAsString,ByVal OccupationAsString,ByVal DescriptionAsString,ByVal ZipAsString,ByVal FirstNameAsString,ByVal LastNameAsString,ByVal YearGraduateAsString,ByVal EmailAsString,ByVal ContactAsBoolean,ByVal original_UserNameAsString,ByVal UaaStudentIdAsString)TryDim connxAsNew SqlConnection(getConnectionString)

connx.Open()

Dim sqlCmdAsNew SqlCommand("cis_UpdateAlumniContact", connx)

sqlCmd.Parameters.Add(

New SqlParameter("@.UserName", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.UserName").Value = original_UserName

sqlCmd.Parameters.Add(

New SqlParameter("@.FirstName", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.FirstName").Value = FirstName

sqlCmd.Parameters.Add(

New SqlParameter("@.LastName", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.LastName").Value = LastName

sqlCmd.Parameters.Add(

New SqlParameter("@.Email", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.Email").Value = Email

sqlCmd.Parameters.Add(

New SqlParameter("@.Street", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.Street").Value = Street

sqlCmd.Parameters.Add(

New SqlParameter("@.City", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.City").Value = City

sqlCmd.Parameters.Add(

New SqlParameter("@.State", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.State").Value = State

sqlCmd.Parameters.Add(

New SqlParameter("@.Occupation", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.Occupation").Value = Occupation

sqlCmd.Parameters.Add(

New SqlParameter("@.Description", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.Description").Value = Description

sqlCmd.Parameters.Add(

New SqlParameter("@.Telephone", SqlDbType.NChar))

sqlCmd.Parameters(

"@.Telephone").Value = Telephone

sqlCmd.Parameters.Add(

New SqlParameter("@.Zip", SqlDbType.NChar))

sqlCmd.Parameters(

"@.Zip").Value = Zip

sqlCmd.Parameters.Add(

New SqlParameter("@.Contact", SqlDbType.Bit))

sqlCmd.Parameters(

"@.Contact").Value = Contact

sqlCmd.Parameters.Add(

New SqlParameter("@.YearGraduate", SqlDbType.NVarChar))

sqlCmd.Parameters(

"@.YearGraduate").Value = YearGraduateDim cmdAs SqlDataReader = sqlCmd.ExecuteReaderCatch exAs ExceptionDim erAsNew cis_ODS_Error

er.InsertError(

"cis_ODS_Alumni - UpdateAlumni: " + ex.Message.ToString)EndTryEndSub

The sproc it calls is:

ALTER PROCEDURE

dbo.cis_UpdateAlumniContact

@.UserName

as nvarchar(50),

@.Street

as nvarchar(50),

@.City

as nvarchar(50),

@.State

as nvarchar(2),

@.Telephone

as nvarchar(50),

@.Occupation

as nvarchar(50),

@.Description

as nvarchar(50),

@.Zip

as nvarchar(50),

@.FirstName

as nvarchar(50),

@.LastName

as nvarchar(50),

@.YearGraduate

as nvarchar(4),

@.Email

as nvarchar(50),

@.Contact

as bit

AS

UPDATEcis_AlumniContact

SETStreet = @.Street, City = @.City, State = @.State, Telephone = @.Telephone, Occupation = @.Occupation, Description = @.Description, Zip = @.Zip, FirstName = @.FirstName, LastName = @.LastName, YearGraduate = @.YearGraduate, Email = @.Email, Contact = @.Contact

FROMaspnet_UsersINNER JOINcis_AlumniContact

ONcis_AlumniContact.UserId = aspnet_Users.UserId

WHERE@.UserName = aspnet_Users.UserName

RETURN

I get this vague error

cis_ODS_Alumni - UpdateAlumni: Incorrect syntax near 'cis_UpdateAlumniContact'

If I execute the SQL from the editor it works fine. The only thing different about this sproc and my other update sprocs is the inner join. Any idea? Thanks

I see two possible problems.

You need to set the command type to:StoredProcedure

You are calling ExecuteReader, I think you need to call ExecuteNonQuery.

|||Holy cow man. Thanks so much. I copied and pasted from another method and didn't include that line. Thanks again.

Monday, March 12, 2012

Invalid Column Name does not cause error

Hi,
I have a SQL Server 2000 sproc that selects data into a temporary table.
If certain conditions are met, the sproc then uses a join on the temporary
table to update an other table.
In the join for this update, the join column on the temporary table is named
incorrectly.
This code is called as part of a sequence of calls to hundreds of sprocs.
The thing is the code has never raised an error, the successful execution of
the sproc is logged to another table. I would expect the code to raise
something like
Server: Msg 207, Level 16, State 3, Line ..
Invalid column name 'dddddddd'.
I am aware of deferred name resolution but I think that when SQL Server
compiles the procedure the error should be raised.
Of course I can edit the sproc but the client requires evidence of
malfunction. Any help appreciated.Could you please post the DDL of the offending procedure?
ML|||The table is created with a column rt_pol_num. The join is the non-existent
column num_pol_dcrt. I have called the sproc with the recompile option but
the error is not raised.
BEGIN
SELECT DISTINCT
rt_pol_num = DCAM.num_pol_dcrt
Blah, Blah …
INTO #tdcam_t
FROM dcam_t dcam
WHERE dcam.stge_rec_err_ind <> 'Y' AND
dcam.prces_act_cd IN('ISRT') AND
EXISTS
(
SELECT src_key_vlu_txt
FROM alternate_payee_t
WHERE alternate_payee_t.src_key_vlu_txt = 'DCAM' +
DCAM.num_pol_dcrt +
DCAM.num_cert_dcrt +
DCAM.ctlnum_reg_cntrl_number +
UPPER(REPLACE(CONVERT(CHAR(11), DCAM.dte_alt_mail_eff_dcam, 106),
' ', ''))
)
SELECT @.vErrNum = @.@.ERROR, @.vIsrtRecCount = @.@.ROWCOUNT, @.vStatrcTblNm =
'dcam_t'
IF @.vErrNum <> 0
BEGIN
SELECT @.vErrLocNm = 'cannot select from dcam_t into #tdcam_t',
@.vErrTblNm = 'dcam_t',
@.vStatDsc = 'procedure failed'
GOTO ON_ERROR
END
ELSE
SELECT @.vDropTmp = 'DROP TABLE #tdcam_t'
IF @.vIsrtRecCount > 0
BEGIN
IF @.pWriteTable <> 'N'
BEGIN
UPDATE dcam_t
SET stge_rec_err_ind = 'Y'
WHERE EXISTS
(
SELECT stge_rec_err_ind
FROM #tdcam_t TT
WHERE TT.num_pol_dcrt = dcam_t.num_pol_dcrt
)
SELECT @.vErrNum = @.@.ERROR, @.vUpdtRecCount = @.@.ROWCOUNT, @.vStatrcTblNm =
'dcam_t'
IF @.vErrNum <> 0
BEGIN
SELECT @.vErrLocNm = 'cannot update dcam_t',
@.vErrTblNm = 'dcam_t',
@.vStatDsc = 'procedure failed'
GOTO ON_ERROR
END|||Non-existing Tables are not schema validated. If the parser notifies a
table which exists the columns are checked for existence, but not if
the table isn=B4t present during compilation time (like in your case)
CREATE PROCEDURE Testproc
AS
BEGIN
CREATE TABLE #testtable
(
SomeColumn int
)
Select somenotexsitingcolumn from #testtable
END
--VS
CREATE TABLE testtable
(
SomeColumn int
)
CREATE PROCEDURE Testproc2
AS
BEGIN
Select somenotexsitingcolumn from testtable
END
HTH, Jens Suessmeyer.|||The OP also states there are no errors at execution.
ML|||Hi,
The error is not raised because during compilation the temporary table does
not exist and SQL Server defers examination of the statement until run-time.
@.vIsrtRecCount > 0
is always false and so the code is never entered. If
@.vIsrtRecCount > 0
is true, the error 'Invalid column name' occurs.
Thanks to all who replied.