1

I am trying to allow users to upload an excel sheet to insert data into the sql database. When they upload data I want a datagrid view to show the data they just uploaded in the database and to be able to see that in retrospect as well based on the date.

I created a table like this:

**dbo.DB_TRACK**  
V_ID Identity  
TBL_N varchar(20)  
ID  int  
ENTR_DT date  

I wrote Stored Procedure for one of the database tables like this:

Create Procedure dbo.Insert_Students  
(  
SName varchar(10),  
SAge varchar(10),  
SAdmit_Dt varchar(10)  
)  
    Declare @id table(id int),  
     @pk int,  
     @iSAge int,  
     @iSAdmit_Dt date  

     Set @iSAge=@SAge  
     Set @iSAdmit_Dt=Convert(Date,@SAdmit_Dt)

    BEGIN TRY  

      BEGIN TRANSACTION  

      Insert into dbo.Student(Name,Age,AdmissionDt)  
      OUTPUT INSERTED.S_ID into @id    
      Values  
      (@SName,@iSAge,@iSAdmit_Dt)  

      COMMIT TRANSACTION  

      BEGIN TRANSACTION  

      Insert into dbo.DB_TRACK (TBL_N, ID, ENTR_DT ) 
      VALUES ('dbo.Student', (Select @pk=id from @id),GETDATE)

      COMMIT TRANSACTION  

    END TRY  
    BEGIN CATCH

      IF @@TRANCOUNT > 0  
          ROLLBACK TRANSACTION  

    END CATCH

I am getting the error:

Incorrect syntax near '='.

UPDATE:
Not getting this error anymore (Thanks to David Browne) and was able to save the SP but when executing the SP, I am getting this error:

Conversion failed when converting date and/or time from character string.

I tried Convert(date,GetDate()) but it did not work

The code that calls this stored procedure is in VBA like this:

Dim SName, SAge,SAdmit_DT as String  
Dim oSName, oSAge,oSAdmit_DT as ADODB.Parameter  

SName= ws.Cells(1, 11)  
SAge = ws.Cells(1, 12)  
SAdmit_DT = ws.Cells(1, 13)  

Set oSName = cmd.CreateParameter("@SName", adVarChar, adParamInput, 10, SName)  
cmd.Parameters.Append oSName  
Set oSAge = cmd.CreateParameter("@SAge", adVarChar, adParamInput, 10, SAge)  
cmd.Parameters.Append oSAge  
Set oSAdmit_DT = cmd.CreateParameter("@SAdmit_DT", adVarChar, adParamInput, 10, SAdmit_DT)  
cmd.Parameters.Append oSAdmit_DT 

cmd.Execute' This is where it errors out

I tried removing SAdmit_Dt but still got the error
I tried passing DATE() through excel but got an Arithmatic overflow error
Any idea how to fix this? Also is there a better way to do what I am trying to do?

3
  • Based on the limited information available, my money is still on SAdmit_Dt being something that can't convert to a date. As a test, I'd create another table Student2 with the AdmissionDT defined as varchar(10). Then change your stored procedure to have SAdmit_Dt also defined as varchar(10) and insert into Student2. Does the test work? If so, what values are in the SAdmit_Dt column in the test table? Oct 23, 2017 at 20:22
  • @ScottHodgin That is what I was doing, I had to update the stored procedure in the question. I had varchar in VBA, varchar in SP, and then I was converting it from varchar to date in the SP. Is that what you meant?
    – SQLserving
    Oct 23, 2017 at 20:36
  • 1
    I see you made a few changes to your original post. How do you know what's in @SAdmit when you execute this Set @iSAdmit_Dt=Convert(Date,@SAdmit_Dt)? To be a good test, you need to change everywhere there is a Date to be varchar to see if that is where the problem is. I honestly don't think the GETDATE stuff is the problem. Oct 23, 2017 at 20:39

1 Answer 1

1

-Please don't use short, cryptic column names.
-There's no reason to wrap individual statements in explicit transactions.

You'll want to extract the key value from the table variable in a separate statement. Something like:

Create Procedure dbo.Insert_Student  
(  
SName varchar(10),  
SAge int,  
SAdmit_Dt date  
)  
BEGIN
    Declare @ids table(id int)
    Declare @pk int 

    BEGIN TRY  

      BEGIN TRANSACTION  

      Insert into dbo.Student(Name,Age,AdmissionDt)  
      OUTPUT INSERTED.S_ID into @id    
      Values  (@SName,@SAge,@SAdmit_Dt);

      set @pk = (Select id from @id);

      Insert into dbo.DB_TRACK (TBL_N, ID, ENTR_DT ) 
      VALUES ('dbo.Student', @pk,GETDATE());

      COMMIT TRANSACTION  

    END TRY  
    BEGIN CATCH

      IF @@TRANCOUNT > 0  
          ROLLBACK TRANSACTION  

      THROW;

    END CATCH
END
4
  • -----I am not getting the error but when I execute it, it gives me "Conversion failed when converting date and/or time from character string." erro
    – SQLserving
    Oct 23, 2017 at 17:35
  • @SQLserving - post the code that you're using to call this stored procedure. What are you passing in for SAdmit_Dt? Have you tried stepping through the debugger to find the failing line of code? Oct 23, 2017 at 17:52
  • @ScottHodgin I have updated the question. I am passing a date to it from within a sheet. The reason I think it is an issue with the sql proc is because where it errors out in vba (at execute). I did run the debugger within sql and manually entered the value and it went through fine.
    – SQLserving
    Oct 23, 2017 at 20:11
  • Thanks David Browne and ScottHodgin. The issue I raised was fixed and the other issue was a glitch in the code which I managed to find today by process of elimination.
    – SQLserving
    Oct 24, 2017 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.