Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 OPENROWSET parms'

Author  Topic 

logmeinsucker@hotmail.com
Starting Member

2 Posts

Posted - 2008-03-14 : 05:48:05
Hi

I've got the following code that uses OPENROWSET but I need to paramaterise the filepaths and its not working:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

BEGIN

declare @dataareaid as varchar(3)
declare @configpath as varchar(100)
declare @configfile as varchar(100)
declare @configformatfile as varchar(100)


set @configfile = 'C:\ConfigFile\ConfigFileMot.txt'
set @configformatfile = 'C:\ConfigFile\ConfigFormatFile.txt'

print @dataareaid
print @configpath
print @configfile
print @configformatfile

set @dataareaid = (SELECT dataareaid
FROM OPENROWSET( BULK @configfile , FORMATFILE = @configformatfile
) AS a)


select *
FROM AX.[P2].dbo.Dimensions Dim
WHERE Dim.dataareaid = @dataareaid

END

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-03-14 : 05:57:19
Try this:


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

BEGIN

declare @dataareaid as varchar(3)
declare @configpath as varchar(100)
declare @configfile as varchar(100)
declare @configformatfile as varchar(100)


set @configfile = 'C:\ConfigFile\ConfigFileMot.txt'
set @configformatfile = 'C:\ConfigFile\ConfigFormatFile.txt'

print @dataareaid
print @configpath
print @configfile
print @configformatfile

exec sp_executesql
N'select @dataareaid = dataareaid FROM OPENROWSET( BULK ''' + @configfile + ''', FORMATFILE = ''' + @configformatfile + ''') as a',
N'@dataareaid varchar(3) output', @dataareaid output

select *
FROM AX.[P2].dbo.Dimensions Dim
WHERE Dim.dataareaid = @dataareaid


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-14 : 06:01:23
Similar dynamic query you can also find it here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

logmeinsucker@hotmail.com
Starting Member

2 Posts

Posted - 2008-03-14 : 08:01:35
Thanks for the replies - brilliant help!

How would I dynamically set the @configpath to the installation directory of SQL Server, something like %homepath% so that if SQL is installed on the D: drive then my SP will still work?
Go to Top of Page
   

- Advertisement -