Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversion proble, in SP

Status
Not open for further replies.

johnrg1

Programmer
Jan 24, 2003
38
GB
Hi all,

I am trying to create a SP that will insert some data into a table, where the table name is provided as a varialbe.

ALTER PROCEDURE InsertStudentAttendance
(
@Date datetime,
@tableName varchar(15)
)
as
DECLARE @myStr varchar(500)
declare @CDATE varchar(25)

set @CDATE = CONVERT(VARCHAR(25), @Date , 102 )

set @myStr = 'insert into [dbo].['+@tableName+'] (Lecture_4) Values (' + @CDATE +')'

EXEC (@myStr)

With the Date set to 102, and many others, i get an error
"invaild SQL statement. Checkthe server filter on the form record course"
with it set to 103 i get a value of 20 (sent it to a char column to check value returned)

I can not understand where the problem is. Anyone see what i am missing?

Cheers

John


 
The whole design seems wrong to me. Why do you have different tables with identical structure?

ALTER PROCEDURE InsertStudentAttendance
(
@Date datetime,
@tableName varchar(15)
)
as
DECLARE @myStr varchar(500)

set @myStr = 'insert into [dbo].['+@tableName+'] (Lecture_4) Values (''' + cast(@DATE as varchar) +''')'

EXEC (@myStr)
 
Hi,

Change @mystr like this

set @myStr = 'insert into [dbo].['+@tableName+'] (Lecture_4) Values (''' + @CDATE +''')'

Does it help

Sunil
 
Thank you all for such a fast response

sunila,

Worked first time.

What a star.

John

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top