473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Using variable in Stored procedure - help!

Hello

I am a newbie to this, so I would appreciate any help, I am struggling
to get this to work

CREATE PROCEDURE [dbo].[sp_test]
@strfinalint as varchar(1000),
@startdate as datetime
@enddate as datetime

as

declare @insertcmd as varchar(2000)
declare @startdate as datetime
declare @enddate as datetime

set @insertcmd = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.
4.0'',''Excel 8.0;Database=d:\MyFolder\' + @strfinalint + ';'',
''SELECT * FROM [Sheet1$]'') Select * from tbltest WHERE S_Date
Between' + @startdate + 'AND' + @enddate

EXEC (@insertcmd)
GO

It was working with the command

set @insertcmd = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.
4.0'',''Excel 8.0;Database=d:\MyFolder\' + @strfinalint + ';'',
''SELECT * FROM [Sheet1$]'') Select * from tbltest'

But I am struggling to include the WHERE part of it, I seem to have
problems making the variables work in this with appropriate quotation
marks !!

Thanks
Sunny

Nov 7 '07 #1
3 2215
(su******@gmail.com) writes:
I am a newbie to this, so I would appreciate any help, I am struggling
to get this to work

CREATE PROCEDURE [dbo].[sp_test]
Don't call your procedures sp_something. The sp_ prefix is reserved
for system procedures, and SQL Server will first look for these in
the master database.
set @insertcmd = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.
4.0'',''Excel 8.0;Database=d:\MyFolder\' + @strfinalint + ';'',
''SELECT * FROM [Sheet1$]'') Select * from tbltest WHERE S_Date
Between' + @startdate + 'AND' + @enddate

EXEC (@insertcmd)
When working with dynamic SQL, it's always a good idea to add:

IF @debug PRINT @sql

so that you can see what you have generated. I bet you will see the
error very quickly in this case!

However, you would have less problems if you used sp_executesql
instead, since in this case you could pass @startdate and @enddate
as parameters:

set @insertcmd = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.
4.0'',''Excel 8.0;Database=d:\MyFolder\' + @strfinalint + ';'',
''SELECT * FROM [Sheet1$]'') Select * from tbltest WHERE S_Date
Between @startdate AND @enddate'
SET @params = '@startdate datetime, @enddate datetime'
EXEC sp_executesql @insertcmd, @params, @startdate, @enddate

Note that you must declare @insertcmd and @params as nvarchar for this
to work.

Note also that you cannot pass @strfinalint as a parameter, but that
variable you need to interpolate into the string.
For more details on sp_executesql and dynamic SQL in general, you may
be interested in an article on my web site:
http://www.sommarskog.se/dynamic_sql.html.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 7 '07 #2
Erland,

Thanks, this gets me closer to what I want to achieve, but because of
the @insertcmd being a varchar or nvarchar, I am having problems
filtering the dates.

CREATE PROCEDURE [dbo].[USP_Test]
@strfinalint as varchar(1000),
@startdate as datetime,
@enddate as datetime
as

declare @insertcmd as nvarchar(2000)
set @insertcmd = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.
4.0'',''Excel 8.0;Database=D:/MyDrive/' + @strfinalint + ';'',
''SELECT * FROM [Sheet1$]'') Select * from VwStaff WHERE S_Date
Between '''+ @startdate + ''' AND '''+ @enddate + ''' '

EXEC (@insertcmd)
GO

Am I doing something wrong? I tried using the @PARAMS but it does not
seem to work, so I tried the above, and now it tells me

Syntax error converting datetime from character string.

Sorry, but I am not too good at this.

Thanks
Sunil

Nov 9 '07 #3
(su******@gmail.com) writes:
Thanks, this gets me closer to what I want to achieve, but because of
the @insertcmd being a varchar or nvarchar, I am having problems
filtering the dates.

CREATE PROCEDURE [dbo].[USP_Test]
@strfinalint as varchar(1000),
@startdate as datetime,
@enddate as datetime
as

declare @insertcmd as nvarchar(2000)
set @insertcmd = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.
4.0'',''Excel 8.0;Database=D:/MyDrive/' + @strfinalint + ';'',
''SELECT * FROM [Sheet1$]'') Select * from VwStaff WHERE S_Date
Between '''+ @startdate + ''' AND '''+ @enddate + ''' '

EXEC (@insertcmd)
GO

Am I doing something wrong? I tried using the @PARAMS but it does not
seem to work,
What you do mean with "does not seem to work"? Did you get any error
message? Did you get unexpected results? Something else? I can't assist,
if I don't know what you are doing.
so I tried the above, and now it tells me

Syntax error converting datetime from character string.
That's because SQL Server has strict rules for data-type precedence, which
says that when two different data types meet, the one with lower
precedence gets converted to the higher. And varchar has lower priority
than datetime, so it's trying to convert the SQL string to datetime.
Which of course does work out.

You need to use convert to explicitly convert the dates to character, but
using parameterised commands is better in my opinion.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi All, I am having problem returning values from a Stored Procedure that creates a dynamic table (table variable) inserts values during a procedure and then I select from that dynamic table to...
0
by: Aaron | last post by:
The following code works fine when previewing a Crystal report using ASP, EXCEPT when it gets to a report using a SubReport and its associated parameters. The whole report just comes up blank with...
5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
7
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page...
4
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will...
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
4
by: onecorp | last post by:
I have a SQL table comprised of 31 columns. The first column is simply an id column, the next 30 columns are labelled ,.... The numerical columns have a tinyint type and the data stored is either...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.