Solved

Must declare the scalar variable

Posted on 2011-09-13
7
1,335 Views
Last Modified: 2012-05-12
The sp that receives the Table name as a parameter.
I keep receiving the error message

Must declare the scalar variable "@ProdID".

I tired many different ways and didn't work.

Please help.

--------------------------------------------------
USE [MyDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[SP_Test]
      -- Add the parameters for the stored procedure here
      @schema sysname,
      @table_name sysname,
      @ProdID varchar(10),
      @EntryDateTime  datetime,
      @SeqNo bigint,
      @File_path varchar (250)
AS
BEGIN
SET @table_name = RTRIM(@table_name)
DECLARE @cmd AS NVARCHAR(max)

SET NOCOUNT ON
SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (@ProdID, @EntryDateTime, SYSDATETIME(),  @SeqNo, @File_path)'
print @cmd

EXEC sp_executesql @cmd
END

GO
-------------------------------------------------------------------
0
Comment
Question by:pcssecure
  • 4
  • 3
7 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 36528839
Hi,

You're trying to use the variable in the dynamic sql, that is not possible. Either use the value directly

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +@EntryDateTime + ''', ''' +SYSDATETIME() + ''', ''' +@@SeqNo + ''', ''' +@@File_path + ''')'

or use

sp_executesql

/peter
0
 
LVL 22

Accepted Solution

by:
pivar earned 500 total points
ID: 36528851
Looking at the datatypes, the line should read:

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +CONVERT(varchar, @EntryDateTime, 101) + ''', ''' +CONVERT(varchar, SYSDATETIME(), 101) + ''', ' +CONVERT(varchar, @SeqNo) + ', ''' +@File_path + ''')'
0
 

Author Comment

by:pcssecure
ID: 36529519
Thanks for the help but still not working from my VB app. But it is working from SQL Server Management Studio. So I guess we are very close to the solution.

            Dim EntryTime As SqlParameter = New SqlParameter("@EntryTime ", SqlDbType.DateTime)
            EntryTime .Value =In_ EntryTime 'Passed in as parameter type DateTime
            myCommand.Parameters.Add(EntryTime )

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

Not sure why should I use convert when I pass in value as Type DateTime?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 22

Expert Comment

by:pivar
ID: 36531851
Dim EntryTime As SqlParameter = New SqlParameter("@EntryTime ", SqlDbType.DateTime) should be
Dim EntryTime As SqlParameter = New SqlParameter("@EntryDateTime ", SqlDbType.DateTime)?

I think the error comes from sql, but I can't see anything obvious, so can you log what the content of @cmd is?
0
 

Author Comment

by:pcssecure
ID: 36533965
Thanks. Problem solved but strange. I need to capture up to milliseconds. So when I use

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +CONVERT(varchar, @EntryDateTime, 121) + ''','''+ CONVERT(varchar, SYSDATETIME(),121) + ''', ' +CONVERT(varchar, @SeqNo) + ', ''' +@File_path + ''')'
Produced Error : Conversion failed when converting date and/or time from character string.

So I changed to

SET @cmd = N'INSERT INTO ' +  QUOTENAME(@schema) + N'.' + QUOTENAME(@table_name) + N' (ProdID, EntryDateTime, InsDateTime, SeqNo,File_path) VALUES (''' +@ProdID + ''', ''' +CONVERT(varchar, @EntryDateTime, 121) + ''', SYSDATETIME() , ' +CONVERT(varchar, @SeqNo) + ', ''' +@File_path + ''')'
And it works fine.
Any idea why caused an error in the first one when using 121 with SYSDATETIME()?
Is using 121 correct in convert function to get up to millisecond?
I got the working one by trial and error.
Thanks.
0
 

Author Comment

by:pcssecure
ID: 36533972
Thanks and hope to here from you.
0
 
LVL 22

Expert Comment

by:pivar
ID: 36534598
I'm guessing now, but it could be that the problem lies in that SYSDATETIME returns a DATETIME2.
You should see ms with 121 (yyyy-mm-dd hh:mi:ss.mmm(24h))
Have you tried another format, perhaps 109 or 113? Or try to convert to DATETIME first?
I'm can not test it myself right now.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Select latest stock count records 11 56
Selecting specific rows 3 50
join tables 4 50
How do I call MySQL Stored Procedure from oracle using HS link ? 5 24
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now