473,396 Members | 1,996 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,396 software developers and data experts.

Variables in SQL Statements

What am I doing wrong in the following snippet of code?

-----------------------------------
declare @FullPath varchar(255)
set @FullPath = 'C:\MyDir\MyFile.txt'

bulk insert Northwind.dbo.Sometable
from @FullPath
with
(FIELDTERMINATOR = ',')
-----------------------------------

This works when I write the path out instead of using the @FullPath
variable. I have a cursor that will Bulk Insert all files names from a
table. The path will not change but the file name will. I guess I could use
a system call to rename the file in use to a hardcoded name and then rename
back to original name but I would like find a way to make the above concept
work if it is possible.

TIA,

-p
Jul 20 '05 #1
2 5350
According the the Books Online syntax, BULK INSERT doesn't take a variable
as a file name. A workaround is to build a SQL statement string and execute
it dynamically using EXEC or sp_executesql. For example:

DECLARE @SqlStatement nvarchar(4000)
SET @SqlStatement =
'
BULK INSERT Northwind.dbo.Sometable
FROM ''' + @FullPath + '''
WITH
(FIELDTERMINATOR = '','')
'

EXEC sp_executesql @SqlStatement

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Pippen" <12*@hotmail.com> wrote in message
news:HGM4d.246840$Fg5.43547@attbi_s53...
What am I doing wrong in the following snippet of code?

-----------------------------------
declare @FullPath varchar(255)
set @FullPath = 'C:\MyDir\MyFile.txt'

bulk insert Northwind.dbo.Sometable
from @FullPath
with
(FIELDTERMINATOR = ',')
-----------------------------------

This works when I write the path out instead of using the @FullPath
variable. I have a cursor that will Bulk Insert all files names from a
table. The path will not change but the file name will. I guess I could
use a system call to rename the file in use to a hardcoded name and then
rename back to original name but I would like find a way to make the above
concept work if it is possible.

TIA,

-p

Jul 20 '05 #2

"Dan Guzman" <gu******@nospam-online.sbcglobal.net> wrote in message
news:Tc******************@newssvr30.news.prodigy.c om...
According the the Books Online syntax, BULK INSERT doesn't take a variable
as a file name. A workaround is to build a SQL statement string and
execute it dynamically using EXEC or sp_executesql. For example:

DECLARE @SqlStatement nvarchar(4000)
SET @SqlStatement =
'
BULK INSERT Northwind.dbo.Sometable
FROM ''' + @FullPath + '''
WITH
(FIELDTERMINATOR = '','')
'

EXEC sp_executesql @SqlStatement

--
Hope this helps.

Dan Guzman
SQL Server MVP


Thank you! Much more elegant that the hack I was considering.

-p
Jul 20 '05 #3

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

Similar topics

15
by: Michael | last post by:
I've written a simple class that puts together a MySQL SELECT query and allows you to extend the query, but I'm unsure as to when to use $this - > var_name and when I can just use $varname, and...
14
by: Rahul Chatterjee | last post by:
Hello All I have an asp page in which I am performing the following 1. Querying a database view 2. Returning rows in to a recordset. 3. Looping thru the recordset and printing the data 4....
2
by: Steve D | last post by:
I've looked all over but can't find a solid answer. I've got a function that runs from a View and when the function runs the first time it is calculating a Temperature for a group of Formulas. ...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
6
by: flaavia | last post by:
Hello My oleDbDataAdapter1 seems to be limited to 99 variables in SQL-statements. At least it does give an error when tying to address more than 99 Does anyboda know if that is a fixed limit...
8
by: Howard Kaikow | last post by:
I got bored today, so I decided to rewrite the code in KB article 316383 to decrease the number of object references. This resulted in a number of nested With ... End With. The original code had...
11
by: | last post by:
Is it possible to define a variable in a block in order to make it invisible outside that block? For example, in C I can write { int a .... } then a will only be available inside the curley...
39
by: Gaijinco | last post by:
I have always felt that you should only declared variables as needed, and implicitily it seems many authors to encourage it, but the other day a friend told me that declaring variables inside a...
3
by: frothpoker | last post by:
Guys, I'm sure this has been asked a million times but I can't seem to formulate a google search that returns what i'm looking for. I've go a dev and live environment. Currently the DB...
6
by: Frank Swarbrick | last post by:
Interesting! I was going to ask if such a thing existed, but I was pretty much convinced they did not so I didn't ask. Looks like with version 9.5 DB2 supports global variables: "Global...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.