SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


OPENROWSET to return an integer with parameters


OPENROWSET to return an integer with parameters

Author
Message
robert-695083
robert-695083
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 25
OPENROWSET to return an integer with parameters


Inside a function, I need to return an Integer. But if I use parameters I have to wrap the whole Openrowset in Dynamic SQL and It becomes a string and I get the following error when I call the function:

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value '
SELECT MAID
FROM OPENROWSET('SQLOLEDB','Server=(local);Trusted_Connection=Yes;Database=CDR;',
'Select Top 1 MAID From MastersAgentsRangeList Where ParentID In ( Select MAID From MastersAgentsRangeList
Where RangeFrom = ''1000'' AND RangeTo = ''5000'') ORDER BY NEWID()' )' to data type int.



Statement used to call function:
Select [dbo].[fn_GetRandomT2Value](1000 ,5000 ) as MAID

This is the function:

ALTER FUNCTION [dbo].[fn_GetRandomT2Value](@RangeFrom Int, @RangeTo Int)
RETURNS Int

AS
BEGIN
DECLARE @SQLString NVARCHAR(500)
DECLARE @MAID Int

Set @MAID =(
N'
SELECT MAID
FROM OPENROWSET(''SQLOLEDB'',''Server=(local);Trusted_Connection=Yes;Database=CDR;'',
''Select Top 1 MAID From MastersAgentsRangeList Where ParentID In ( Select MAID From MastersAgentsRangeList
Where RangeFrom = ''''' + Cast(@RangeFrom as nvarchar) + ''''' AND RangeTo = ''''' + Cast(@RangeTo as nvarchar) + ''''') ORDER BY NEWID()'' )'
)

RETURN @MAID

END
Ben Teraberry
Ben Teraberry
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1491 Visits: 1199
My first question ... why are you using OPENROWSET to query a database on the local server? Even if you're hitting a remote server, the function is really designed for ad hoc queries. Otherwise it is better to use linked servers.

└> bt


Forum Etiquette: How to post data/code on a forum to get the best help
robert-695083
robert-695083
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 25
Seems you cant use Order By new() in a function unless it is in Dynamic sql. And the only way I could find to use it was in openrowset...Any other idea?
Ben Teraberry
Ben Teraberry
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1491 Visits: 1199
NEWID() is to create a new unique identifier ... ordering by that function makes no sense to me. I would be curious to understand exactly what you're trying to accomplish in terms of the data you're working with and the results you're after.

└> bt


Forum Etiquette: How to post data/code on a forum to get the best help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84627 Visits: 41067
bteraberry (4/6/2010)
NEWID() is to create a new unique identifier ... ordering by that function makes no sense to me. I would be curious to understand exactly what you're trying to accomplish in terms of the data you're working with and the results you're after.


It produces a result set in random order.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84627 Visits: 41067
robert-695083 (4/6/2010)
Seems you cant use Order By new() in a function unless it is in Dynamic sql. And the only way I could find to use it was in openrowset...Any other idea?


You can trick it, though. Create a view with nothing in it but SELECT NEWID() AS SomeNewID and you can call the view from the function.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search