Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Thursday, March 8, 2012

Creating a Group By without functions -- possible?

Hi all,

I'm trying to create a layout of our website for Marketing to review,
and though I know how I want it presented, I'm not sure how to write
the SQL code to make it work. Here's a sample of the View I've written
with all our content:

Level Title ID ParentID
1 Clinics 1
1 Services 2
2 Surgery 1 2
2 ER 2 2
2 Radiology 3 2
2 Clinic 1 4 1
2 Clinic 2 5 1
2 Clinic 3 6 1
3 Heart 1 1
3 Lung 2 1
3 Physicians 3 4
3 Physicians 4 5
3 Physicians 5 6

And the output would basically be this:

Clinics
- Clinic 1
-- Physicians
- Clinic 2
-- Physicians
- Clinic 3
-- Physicians

Services
- Surgery
-- Heart
-- Lung
- ER
- Radilogy

Is there anyway to do this in a SQL statement, or maybe Crystal or
Access? I even tried a Pivot table in Excel, but it wants to sum
stuff, and I'm not working with numbers (sums, counts, etc). Also
since I'm not using any aggrate functions MS SQL is complaining when i
use Group By.

Thanks for any suggestions. Also though the layout is similar, the
items listed above are pulled outta the air. Hopefully I typed up
something that makes since :)

AlexGoogle Nested Sets Model for trees and buy a copy of TREES &
HIERARCHIES IN SQL.|||create table tree(id int, parentID int, nodename varchar(20))
insert into tree values(1,null,'CEO')
insert into tree values(2,1,'leader of team 1')
insert into tree values(3,1,'leader of team 2')
insert into tree values(4,2,'Jerry, team 1')
insert into tree values(5,2,'Cho, team 1')
insert into tree values(6,3,'Raj, team 2')
insert into tree values(7,3,'Kim, team 2')
select * from tree

id parentID nodename
---- ---- -------
1 NULL CEO
2 1 leader of team 1
3 1 leader of team 2
4 2 Jerry, team 1
5 2 Cho, team 1
6 3 Raj, team 2
7 3 Kim, team 2

(7 row(s) affected)

go
create procedure dbo.display_tree
as
begin
declare @.level int, @.inserted int

set nocount on

create table #tree(path varchar(20), dashes varchar(20), tree_level
int,
id int, nodename varchar(20))

insert into #tree select convert(varchar(20), id), '', 1, id, nodename
from tree where parentID is null

set @.level = 1
set @.inserted = 1

while (@.inserted > 0) and (@.level < 10)
begin
insert into #tree
select path + '.' + convert(varchar(20), tree.id), dashes + '-',
@.level+1,
tree.id, tree.nodename
from #tree, tree
where #tree.tree_level = @.level
and #tree.id = tree.parentID

set @.inserted = @.@.rowcount
set @.level = @.level + 1
end

select dashes+nodename from #tree order by path
drop table #tree
end
go
exec dbo.display_tree

------------
CEO
-leader of team 1
--Jerry, team 1
--Cho, team 1
-leader of team 2
--Raj, team 2
--Kim, team 2

go
drop procedure dbo.display_tree
drop table tree

Tuesday, February 14, 2012

Create view of inline function

Hello. I'm a real newbie - using Access 2003 front end and connecting to SQL Server 2005 ODBC.

I'm having trouble accessing functions through access. I've built the following function:

CREATE FUNCTION fnSTR_LEASESTATUS(@.TRS nvarchar(12))

RETURNS TABLE

AS

RETURN

(

SELECT dbo.tblTRACT.STR, dbo.tblTRACT.[TRACT_#], dbo.tblMIN_OWNERS.Min_Owner_Name AS [OWNER OF RECORD], dbo.tblLEASE_TRACTS.LOC_ID, dbo.tblLOCATION.LPR_No, dbo.tblLOCATION.Lease_ID, dbo.tblLEASE_LOG.Date_Mailed, dbo.tblLEASE_LOG.Scan_Lease_Received, dbo.tblLEASE_LOG.Orig_Lease_Recd, dbo.tblLPR_INVOICES.Invoice_No, dbo.tblLPR_PAY.CHECK_DRAFT_No, dbo.tblLESSORS.Name AS [Lease Name]

FROM dbo.tblTRACT LEFT JOIN ((dbo.tblMIN_OWNERS RIGHT JOIN dbo.tblTRACT_OWNER ON dbo.tblMIN_OWNERS.Min_Owner_ID = dbo.tblTRACT_OWNER.Owner_Lease) LEFT JOIN ((((((dbo.tblLPR RIGHT JOIN dbo.tblLOCATION ON dbo.tblLPR.LPR_No = dbo.tblLOCATION.LPR_No) LEFT JOIN dbo.tblLESSORS ON dbo.tblLPR.Lessor_Number = dbo.tblLESSORS.Lessor_Number) RIGHT JOIN dbo.tblLEASE_TRACTS ON dbo.tblLOCATION.LOC_ID = dbo.tblLEASE_TRACTS.LOC_ID) LEFT JOIN dbo.tblLEASE_LOG ON dbo.tblLPR.LPR_No = dbo.tblLEASE_LOG.LPR_No) LEFT JOIN dbo.tblLPR_INVOICES ON dbo.tblLPR.LPR_No = dbo.tblLPR_INVOICES.LPR_No) LEFT JOIN dbo.tblLPR_PAY ON dbo.tblLPR.LPR_No = dbo.tblLPR_PAY.LPR_No) ON dbo.tblTRACT_OWNER.TRACT__Owner_ID = dbo.tblLEASE_TRACTS.Tract_Owner_Id) ON (dbo.tblTRACT.[TRACT_#] = dbo.tblTRACT_OWNER.[TRACT_#]) AND (dbo.tblTRACT.STR = dbo.tblTRACT_OWNER.STR)

WHERE (((dbo.tblTRACT.STR)=@.TRS))

)

GO

I understand now I can create a view of the function Simply by using the function name in my FROM statement. However I get an error that arguments provided do not match parameters required. However, I'm not getting the prompt to enter my criterion. Is my error in my function statement? I can't save the view. I also understand I could use a pass-through query. Is there some sort of guidance or tutorial on that to which you could point me?

Thanks for your time.

You function has param @.TRS.

You could use it:

Code Snippet

SELECT * FROM fnSTR_LEASESTATUS('Your param value')

|||

Hi,

Let me try to address your concerns

There's no need to create a view of a table function, unless you want the view results somehow filtered or different than the function results.

To use the your table function simply SELECT * FROM fnSTR_LEASESTATUS(<put value here>)

If you want to create a view you need to have rights on the SQL Server 2005 database.

You are probably using an Access data project? Using these projects you are not able to just save queries as in regular Access database. With these projects you are connected directly to the server and therefore anything you save you are saving on the server.

You may want one project to create server objects like functions. Then have another regualr access database to save your queries (pass- through preferred)

|||

That allowed me to run it, but when I try to save it I get the message "Must declare the scalar variable @.TRS".

|||

Ok. I can run it in SQL Server, but how do I get to it in Access? I was hoping I could save it as a view and then link to it the way I have my other views.

Thanks.

|||

Oh, sorry. Forgot to tell you. I decided against ADP after reading on the Access user's group. So I build my views in SQL Server and then link to them.

Thanks.

|||To save your view in SQL Server you need to replace the variable reference, @.TRS, with an actual value. Once you have it saved in SQL Server, you can link like other view links|||And if you want to be prompted from Access, forget saving it as a view in SQL Server and try saving it as an Access query or some other way in Access

Create View based on User-Defined Function

I would like to create a View in SQL Server 2000, based on a user-defined
function. However when I try it, the "Functions" tab is empty. There are no
functions to choose from. I can select tables and other views.
Josh
Well , have you already created UDF?
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:4A014F2F-6429-4564-B0CD-A8896D5C1882@.microsoft.com...
>I would like to create a View in SQL Server 2000, based on a user-defined
> function. However when I try it, the "Functions" tab is empty. There are
> no
> functions to choose from. I can select tables and other views.
|||Yes. I have 8 of them.
"Uri Dimant" wrote:

> Josh
> Well , have you already created UDF?
> "Josh" <Josh@.discussions.microsoft.com> wrote in message
> news:4A014F2F-6429-4564-B0CD-A8896D5C1882@.microsoft.com...
>
>
|||The problem is with your tool of choice. Use QA and just type the
appropriate create/alter view query yourself.
|||Josh
I have no problems. Perhpas if you are using EM , you need to refresh
thisn tool im order to be able to see the functions
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:433B1DEC-B7AB-4C90-A9B1-932CCDE4EE3F@.microsoft.com...[vbcol=seagreen]
> Yes. I have 8 of them.
> "Uri Dimant" wrote:
|||Yeah, I've tried that but the function I'm trying to use contains variables
and the QA throws errors when I try to do it that way.
My ultimate goal is to use the function in MS Access. I was thinking that
I'd need to convert it to a view before I could import or link to it. If
there's another way I'd love to hear about it (I've posted this portion of
the question in one of the Access newsgroups).
Josh
"Scott Morris" wrote:

> The problem is with your tool of choice. Use QA and just type the
> appropriate create/alter view query yourself.
>
>
|||> Yeah, I've tried that but the function I'm trying to use contains
> variables
> and the QA throws errors when I try to do it that way.
It helps to tell us exactly what you are doing. It is also critical to tell
us exactly what "throws errors" means. When you encounter an error, post
the exact text of the error. And get in the practice of specifying which
version (and service pack level) of sql server you are using.

> My ultimate goal is to use the function in MS Access. I was thinking that
> I'd need to convert it to a view before I could import or link to it. If
> there's another way I'd love to hear about it (I've posted this portion of
> the question in one of the Access newsgroups).
> Josh
A view is effectively a pre-cannned select query. You cannot define a view
that will accept arguments and pass those arguments to the function.
|||OK. Hope this will make sense to people unfamiliar with our specific
database. We have a table with a "Comments" field that contains several
pieces of information delimited by slashes (/). I didn't design it this way
and would not have; I'd have put each piece of data in its own field. But
what's done is done and I have to work with it. The DBA created functions to
extract the individual portions of this field. One of them has the syntax
below:
CREATE FUNCTION dbo.GetFirstSlash
( @.Comments varchar(300) )
RETURNS INT
AS
BEGIN
DECLARE @.Result int
SET @.Result = 0
WHILE @.Result < LEN(LTRIM(RTRIM(@.Comments)))
BEGIN
IF SUBSTRING(@.Comments, @.Result, 1) = '/'
BEGIN
BREAK
END
SET @.Result = @.Result + 1
END
RETURN (@.Result)
END
When I try to create a view based on this function in QA by changing "Create
Function" to "Create View," I get the following errors:
Server: Msg 170, Level 15, State 1, Procedure GetFirstSlash, Line 2
Line 2: Incorrect syntax near '@.Comments'.
Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 11
Must declare the variable '@.Comments'.
Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 13
Must declare the variable '@.Comments'.
Server: Msg 178, Level 15, State 1, Procedure GetFirstSlash, Line 21
A RETURN statement with a return value cannot be used in this context.
"Scott Morris" wrote:

> It helps to tell us exactly what you are doing. It is also critical to tell
> us exactly what "throws errors" means. When you encounter an error, post
> the exact text of the error. And get in the practice of specifying which
> version (and service pack level) of sql server you are using.
>
> A view is effectively a pre-cannned select query. You cannot define a view
> that will accept arguments and pass those arguments to the function.
>
>
|||A view is a different thing compared to a function. A view cannot take parameters, not can it
contain code, except for one SELECT statement. In addition, your function isn't even a table values
function, it is a scalar function. A view exposes a set (a table) where a scalar function returns a
scalar value when executed.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:C2DF362C-89AA-4F3D-A5CD-BDEE6F1A866B@.microsoft.com...[vbcol=seagreen]
> OK. Hope this will make sense to people unfamiliar with our specific
> database. We have a table with a "Comments" field that contains several
> pieces of information delimited by slashes (/). I didn't design it this way
> and would not have; I'd have put each piece of data in its own field. But
> what's done is done and I have to work with it. The DBA created functions to
> extract the individual portions of this field. One of them has the syntax
> below:
> CREATE FUNCTION dbo.GetFirstSlash
> ( @.Comments varchar(300) )
> RETURNS INT
> AS
> BEGIN
> DECLARE @.Result int
> SET @.Result = 0
> WHILE @.Result < LEN(LTRIM(RTRIM(@.Comments)))
> BEGIN
> IF SUBSTRING(@.Comments, @.Result, 1) = '/'
> BEGIN
> BREAK
> END
> SET @.Result = @.Result + 1
> END
> RETURN (@.Result)
> END
> When I try to create a view based on this function in QA by changing "Create
> Function" to "Create View," I get the following errors:
> Server: Msg 170, Level 15, State 1, Procedure GetFirstSlash, Line 2
> Line 2: Incorrect syntax near '@.Comments'.
> Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 11
> Must declare the variable '@.Comments'.
> Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 13
> Must declare the variable '@.Comments'.
> Server: Msg 178, Level 15, State 1, Procedure GetFirstSlash, Line 21
> A RETURN statement with a return value cannot be used in this context.
> "Scott Morris" wrote:
|||Once again - a view does not have arguments. You cannot simply replace
"create function" with "create view" in this case. Personally, I recommend
a different approach. First, write a script that contains a select query
that does what you want. Then, try to convert that to a view.
Seems to me that you probably want a view that is based on the table
containing the comments column and that uses some function (or logic) that
parses the comment column into its individual components.

Create View based on User-Defined Function

I would like to create a View in SQL Server 2000, based on a user-defined
function. However when I try it, the "Functions" tab is empty. There are no
functions to choose from. I can select tables and other views.Josh
Well , have you already created UDF?
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:4A014F2F-6429-4564-B0CD-A8896D5C1882@.microsoft.com...
>I would like to create a View in SQL Server 2000, based on a user-defined
> function. However when I try it, the "Functions" tab is empty. There are
> no
> functions to choose from. I can select tables and other views.|||Yes. I have 8 of them.
"Uri Dimant" wrote:
> Josh
> Well , have you already created UDF?
> "Josh" <Josh@.discussions.microsoft.com> wrote in message
> news:4A014F2F-6429-4564-B0CD-A8896D5C1882@.microsoft.com...
> >I would like to create a View in SQL Server 2000, based on a user-defined
> > function. However when I try it, the "Functions" tab is empty. There are
> > no
> > functions to choose from. I can select tables and other views.
>
>|||The problem is with your tool of choice. Use QA and just type the
appropriate create/alter view query yourself.|||Josh
I have no problems. Perhpas if you are using EM , you need to refresh
thisn tool im order to be able to see the functions
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:433B1DEC-B7AB-4C90-A9B1-932CCDE4EE3F@.microsoft.com...
> Yes. I have 8 of them.
> "Uri Dimant" wrote:
>> Josh
>> Well , have you already created UDF?
>> "Josh" <Josh@.discussions.microsoft.com> wrote in message
>> news:4A014F2F-6429-4564-B0CD-A8896D5C1882@.microsoft.com...
>> >I would like to create a View in SQL Server 2000, based on a
>> >user-defined
>> > function. However when I try it, the "Functions" tab is empty. There
>> > are
>> > no
>> > functions to choose from. I can select tables and other views.
>>|||Yeah, I've tried that but the function I'm trying to use contains variables
and the QA throws errors when I try to do it that way.
My ultimate goal is to use the function in MS Access. I was thinking that
I'd need to convert it to a view before I could import or link to it. If
there's another way I'd love to hear about it (I've posted this portion of
the question in one of the Access newsgroups).
Josh
"Scott Morris" wrote:
> The problem is with your tool of choice. Use QA and just type the
> appropriate create/alter view query yourself.
>
>|||> Yeah, I've tried that but the function I'm trying to use contains
> variables
> and the QA throws errors when I try to do it that way.
It helps to tell us exactly what you are doing. It is also critical to tell
us exactly what "throws errors" means. When you encounter an error, post
the exact text of the error. And get in the practice of specifying which
version (and service pack level) of sql server you are using.
> My ultimate goal is to use the function in MS Access. I was thinking that
> I'd need to convert it to a view before I could import or link to it. If
> there's another way I'd love to hear about it (I've posted this portion of
> the question in one of the Access newsgroups).
> Josh
A view is effectively a pre-cannned select query. You cannot define a view
that will accept arguments and pass those arguments to the function.|||OK. Hope this will make sense to people unfamiliar with our specific
database. We have a table with a "Comments" field that contains several
pieces of information delimited by slashes (/). I didn't design it this way
and would not have; I'd have put each piece of data in its own field. But
what's done is done and I have to work with it. The DBA created functions to
extract the individual portions of this field. One of them has the syntax
below:
CREATE FUNCTION dbo.GetFirstSlash
( @.Comments varchar(300) )
RETURNS INT
AS
BEGIN
DECLARE @.Result int
SET @.Result = 0
WHILE @.Result < LEN(LTRIM(RTRIM(@.Comments)))
BEGIN
IF SUBSTRING(@.Comments, @.Result, 1) = '/'
BEGIN
BREAK
END
SET @.Result = @.Result + 1
END
RETURN (@.Result)
END
When I try to create a view based on this function in QA by changing "Create
Function" to "Create View," I get the following errors:
Server: Msg 170, Level 15, State 1, Procedure GetFirstSlash, Line 2
Line 2: Incorrect syntax near '@.Comments'.
Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 11
Must declare the variable '@.Comments'.
Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 13
Must declare the variable '@.Comments'.
Server: Msg 178, Level 15, State 1, Procedure GetFirstSlash, Line 21
A RETURN statement with a return value cannot be used in this context.
"Scott Morris" wrote:
> > Yeah, I've tried that but the function I'm trying to use contains
> > variables
> > and the QA throws errors when I try to do it that way.
> It helps to tell us exactly what you are doing. It is also critical to tell
> us exactly what "throws errors" means. When you encounter an error, post
> the exact text of the error. And get in the practice of specifying which
> version (and service pack level) of sql server you are using.
> > My ultimate goal is to use the function in MS Access. I was thinking that
> > I'd need to convert it to a view before I could import or link to it. If
> > there's another way I'd love to hear about it (I've posted this portion of
> > the question in one of the Access newsgroups).
> >
> > Josh
> A view is effectively a pre-cannned select query. You cannot define a view
> that will accept arguments and pass those arguments to the function.
>
>|||A view is a different thing compared to a function. A view cannot take parameters, not can it
contain code, except for one SELECT statement. In addition, your function isn't even a table values
function, it is a scalar function. A view exposes a set (a table) where a scalar function returns a
scalar value when executed.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:C2DF362C-89AA-4F3D-A5CD-BDEE6F1A866B@.microsoft.com...
> OK. Hope this will make sense to people unfamiliar with our specific
> database. We have a table with a "Comments" field that contains several
> pieces of information delimited by slashes (/). I didn't design it this way
> and would not have; I'd have put each piece of data in its own field. But
> what's done is done and I have to work with it. The DBA created functions to
> extract the individual portions of this field. One of them has the syntax
> below:
> CREATE FUNCTION dbo.GetFirstSlash
> ( @.Comments varchar(300) )
> RETURNS INT
> AS
> BEGIN
> DECLARE @.Result int
> SET @.Result = 0
> WHILE @.Result < LEN(LTRIM(RTRIM(@.Comments)))
> BEGIN
> IF SUBSTRING(@.Comments, @.Result, 1) = '/'
> BEGIN
> BREAK
> END
> SET @.Result = @.Result + 1
> END
> RETURN (@.Result)
> END
> When I try to create a view based on this function in QA by changing "Create
> Function" to "Create View," I get the following errors:
> Server: Msg 170, Level 15, State 1, Procedure GetFirstSlash, Line 2
> Line 2: Incorrect syntax near '@.Comments'.
> Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 11
> Must declare the variable '@.Comments'.
> Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 13
> Must declare the variable '@.Comments'.
> Server: Msg 178, Level 15, State 1, Procedure GetFirstSlash, Line 21
> A RETURN statement with a return value cannot be used in this context.
> "Scott Morris" wrote:
>> > Yeah, I've tried that but the function I'm trying to use contains
>> > variables
>> > and the QA throws errors when I try to do it that way.
>> It helps to tell us exactly what you are doing. It is also critical to tell
>> us exactly what "throws errors" means. When you encounter an error, post
>> the exact text of the error. And get in the practice of specifying which
>> version (and service pack level) of sql server you are using.
>> > My ultimate goal is to use the function in MS Access. I was thinking that
>> > I'd need to convert it to a view before I could import or link to it. If
>> > there's another way I'd love to hear about it (I've posted this portion of
>> > the question in one of the Access newsgroups).
>> >
>> > Josh
>> A view is effectively a pre-cannned select query. You cannot define a view
>> that will accept arguments and pass those arguments to the function.
>>|||Once again - a view does not have arguments. You cannot simply replace
"create function" with "create view" in this case. Personally, I recommend
a different approach. First, write a script that contains a select query
that does what you want. Then, try to convert that to a view.
Seems to me that you probably want a view that is based on the table
containing the comments column and that uses some function (or logic) that
parses the comment column into its individual components.|||On Wed, 27 Sep 2006 09:20:02 -0700, Josh wrote:
>CREATE FUNCTION dbo.GetFirstSlash
> ( @.Comments varchar(300) )
>RETURNS INT
>AS
>BEGIN
>DECLARE @.Result int
>SET @.Result = 0
>WHILE @.Result < LEN(LTRIM(RTRIM(@.Comments)))
> BEGIN
> IF SUBSTRING(@.Comments, @.Result, 1) = '/'
> BEGIN
> BREAK
> END
> SET @.Result = @.Result + 1
> END
>RETURN (@.Result)
>END
Hi Josh,
I know that this is not what you're asking, but exactly how does the
result of this function differ from
CHARINDEX('/', @.Comments)
?
--
Hugo Kornelis, SQL Server MVP

Create View based on User-Defined Function

I would like to create a View in SQL Server 2000, based on a user-defined
function. However when I try it, the "Functions" tab is empty. There are n
o
functions to choose from. I can select tables and other views.Josh
Well , have you already created UDF?
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:4A014F2F-6429-4564-B0CD-A8896D5C1882@.microsoft.com...
>I would like to create a View in SQL Server 2000, based on a user-defined
> function. However when I try it, the "Functions" tab is empty. There are
> no
> functions to choose from. I can select tables and other views.|||Yes. I have 8 of them.
"Uri Dimant" wrote:

> Josh
> Well , have you already created UDF?
> "Josh" <Josh@.discussions.microsoft.com> wrote in message
> news:4A014F2F-6429-4564-B0CD-A8896D5C1882@.microsoft.com...
>
>|||The problem is with your tool of choice. Use QA and just type the
appropriate create/alter view query yourself.|||Josh
I have no problems. Perhpas if you are using EM , you need to refresh
thisn tool im order to be able to see the functions
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:433B1DEC-B7AB-4C90-A9B1-932CCDE4EE3F@.microsoft.com...[vbcol=seagreen]
> Yes. I have 8 of them.
> "Uri Dimant" wrote:
>|||Yeah, I've tried that but the function I'm trying to use contains variables
and the QA throws errors when I try to do it that way.
My ultimate goal is to use the function in MS Access. I was thinking that
I'd need to convert it to a view before I could import or link to it. If
there's another way I'd love to hear about it (I've posted this portion of
the question in one of the Access newsgroups).
Josh
"Scott Morris" wrote:

> The problem is with your tool of choice. Use QA and just type the
> appropriate create/alter view query yourself.
>
>|||> Yeah, I've tried that but the function I'm trying to use contains
> variables
> and the QA throws errors when I try to do it that way.
It helps to tell us exactly what you are doing. It is also critical to tell
us exactly what "throws errors" means. When you encounter an error, post
the exact text of the error. And get in the practice of specifying which
version (and service pack level) of sql server you are using.

> My ultimate goal is to use the function in MS Access. I was thinking that
> I'd need to convert it to a view before I could import or link to it. If
> there's another way I'd love to hear about it (I've posted this portion of
> the question in one of the Access newsgroups).
> Josh
A view is effectively a pre-cannned select query. You cannot define a view
that will accept arguments and pass those arguments to the function.|||OK. Hope this will make sense to people unfamiliar with our specific
database. We have a table with a "Comments" field that contains several
pieces of information delimited by slashes (/). I didn't design it this way
and would not have; I'd have put each piece of data in its own field. But
what's done is done and I have to work with it. The DBA created functions t
o
extract the individual portions of this field. One of them has the syntax
below:
CREATE FUNCTION dbo.GetFirstSlash
( @.Comments varchar(300) )
RETURNS INT
AS
BEGIN
DECLARE @.Result int
SET @.Result = 0
WHILE @.Result < LEN(LTRIM(RTRIM(@.Comments)))
BEGIN
IF SUBSTRING(@.Comments, @.Result, 1) = '/'
BEGIN
BREAK
END
SET @.Result = @.Result + 1
END
RETURN (@.Result)
END
When I try to create a view based on this function in QA by changing "Create
Function" to "Create View," I get the following errors:
Server: Msg 170, Level 15, State 1, Procedure GetFirstSlash, Line 2
Line 2: Incorrect syntax near '@.Comments'.
Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 11
Must declare the variable '@.Comments'.
Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 13
Must declare the variable '@.Comments'.
Server: Msg 178, Level 15, State 1, Procedure GetFirstSlash, Line 21
A RETURN statement with a return value cannot be used in this context.
"Scott Morris" wrote:

> It helps to tell us exactly what you are doing. It is also critical to te
ll
> us exactly what "throws errors" means. When you encounter an error, post
> the exact text of the error. And get in the practice of specifying which
> version (and service pack level) of sql server you are using.
>
> A view is effectively a pre-cannned select query. You cannot define a vie
w
> that will accept arguments and pass those arguments to the function.
>
>|||A view is a different thing compared to a function. A view cannot take param
eters, not can it
contain code, except for one SELECT statement. In addition, your function is
n't even a table values
function, it is a scalar function. A view exposes a set (a table) where a sc
alar function returns a
scalar value when executed.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Josh" <Josh@.discussions.microsoft.com> wrote in message
news:C2DF362C-89AA-4F3D-A5CD-BDEE6F1A866B@.microsoft.com...[vbcol=seagreen]
> OK. Hope this will make sense to people unfamiliar with our specific
> database. We have a table with a "Comments" field that contains several
> pieces of information delimited by slashes (/). I didn't design it this w
ay
> and would not have; I'd have put each piece of data in its own field. But
> what's done is done and I have to work with it. The DBA created functions
to
> extract the individual portions of this field. One of them has the syntax
> below:
> CREATE FUNCTION dbo.GetFirstSlash
> ( @.Comments varchar(300) )
> RETURNS INT
> AS
> BEGIN
> DECLARE @.Result int
> SET @.Result = 0
> WHILE @.Result < LEN(LTRIM(RTRIM(@.Comments)))
> BEGIN
> IF SUBSTRING(@.Comments, @.Result, 1) = '/'
> BEGIN
> BREAK
> END
> SET @.Result = @.Result + 1
> END
> RETURN (@.Result)
> END
> When I try to create a view based on this function in QA by changing "Crea
te
> Function" to "Create View," I get the following errors:
> Server: Msg 170, Level 15, State 1, Procedure GetFirstSlash, Line 2
> Line 2: Incorrect syntax near '@.Comments'.
> Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 11
> Must declare the variable '@.Comments'.
> Server: Msg 137, Level 15, State 1, Procedure GetFirstSlash, Line 13
> Must declare the variable '@.Comments'.
> Server: Msg 178, Level 15, State 1, Procedure GetFirstSlash, Line 21
> A RETURN statement with a return value cannot be used in this context.
> "Scott Morris" wrote:
>|||Once again - a view does not have arguments. You cannot simply replace
"create function" with "create view" in this case. Personally, I recommend
a different approach. First, write a script that contains a select query
that does what you want. Then, try to convert that to a view.
Seems to me that you probably want a view that is based on the table
containing the comments column and that uses some function (or logic) that
parses the comment column into its individual components.

CREATE via Dynamic SQL into new database?

From a stored procedure running in the context of one database, I would like
to create a set of objects (stored procedures, functions, views, users) into
a newly-created second database, where the name is dynamically determined.
Creating the new database and retrieving its name is no problem, the problem
is executing CREATE FUNCTION, CREATE PROCEDURE, etc. in the context of the
new database.
As you know, executing dynamic SQL 'use database' won't change the context
of an executing procedure. And 'use database; create function ...' doesn't
work, because the create statements need to be in their own batch. I cannot
store the objects in Master, so I can't have them automatically created with
the new database.
Is there a way to copy the objects from an existing (i.e. template) database
to the new one using dynamic SQL? Any way to attach a copy of a template
database file to a new database dynamically?
Or any out-of-the-box ideas?declare @.sql nvarchar(1000)
set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
exec sp_executesql @.sql

> From a stored procedure running in the context of one database, I would
> like to create a set of objects (stored procedures, functions, views,
> users) into a newly-created second database, where the name is dynamically
> determined. Creating the new database and retrieving its name is no
> problem, the problem is executing CREATE FUNCTION, CREATE PROCEDURE, etc.
> in the context of the new database.
> As you know, executing dynamic SQL 'use database' won't change the context
> of an executing procedure. And 'use database; create function ...' doesn't
> work, because the create statements need to be in their own batch. I
> cannot store the objects in Master, so I can't have them automatically
> created with the new database.
> Is there a way to copy the objects from an existing (i.e. template)
> database to the new one using dynamic SQL? Any way to attach a copy of a
> template database file to a new database dynamically?
> Or any out-of-the-box ideas?
new|||here's a real hum-dinger: (this is all on one line)
exec opendatasource('sqloledb', 'data
source=YourServer;uid=UserId;pwd=Passwor
d').YourDatabase.dbo.sp_executesql
N'create table mydatabase.dbo.newtable (myfield1 int)'
You'll want to change the following areas:
YourServer
UserId
Password
YourDatabase
.. and the statement of course

> declare @.sql nvarchar(1000)
> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> exec sp_executesql @.sql
>
>
new|||Thanks, but this isn't the issue. Issue is that from a stored procedure (or
batch, for that matter) running in the context of database A, do:
declare @.DBName varchar(20)
set @.DBName = 'dynamic'
declare @.SQL varchar(200)
set @.SQL = 'use ' + @.DBName + '; create function foo ...'
exec (@.SQL)
Doesn't work because 'create function' must be at the beginning of a batch.
set @.SQL = 'create function ' + @.DBName + '.dbo.foo ...' doesn't work by
design.
Need to create functions, stored procs etc. in a different,
dynamically-determined database.
"beginthreadex" wrote:

> declare @.sql nvarchar(1000)
> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> exec sp_executesql @.sql
>
> --
> new
>|||LOL ... next it will be sp_cmdshell(osql ... ). :-)
"beginthreadex" wrote:

> here's a real hum-dinger: (this is all on one line)
> exec opendatasource('sqloledb', 'data
> source=YourServer;uid=UserId;pwd=Passwor
d').YourDatabase.dbo.sp_executesql
> N'create table mydatabase.dbo.newtable (myfield1 int)'
> You'll want to change the following areas:
> YourServer
> UserId
> Password
> YourDatabase
> ... and the statement of course
>
> --
> new
>|||The code I provided does execute the code in the other database. Hence, the
"mydatabase" reference. So, here's your code mixed with mine:
declare @.DBName varchar(20)
set @.DBName = 'dynamic'
declare @.SQL varchar(200)
set @.SQL = 'create function [' + @.DBName + '].dbo.foo ...'
exec sp_executesql @.sql
If there is something else that is confusing please let me know. Because I'm
referencing the database name this will run for the context of the other
database.
;)
[vbcol=seagreen]
> Thanks, but this isn't the issue. Issue is that from a stored procedure
> (or batch, for that matter) running in the context of database A, do:
> declare @.DBName varchar(20)
> set @.DBName = 'dynamic'
> declare @.SQL varchar(200)
> set @.SQL = 'use ' + @.DBName + '; create function foo ...'
> exec (@.SQL)
> Doesn't work because 'create function' must be at the beginning of a
> batch.
> set @.SQL = 'create function ' + @.DBName + '.dbo.foo ...' doesn't work by
> design.
> Need to create functions, stored procs etc. in a different,
> dynamically-determined database.
>|||If you tried it (in s2k), you would realize that you cannot use 3 part
naming for creating procedures or functions. These statements are limited
to accepting an owner name (optional) and an object name.
Try the following statement:
create procedure pubs.dbo.junk as select getdate()|||I deeply apologize! The "Create Table" code does allow for this.
However this DOES work as I have just tested:
exec opendatasource('sqloledb', 'data
source=MySource;uid=MyUID;pwd=MyPWD').pubs.dbo.sp_execsql N'create
procedure dbo.junk as select getdate()'
I know it's not the prettiest, but it DOES work.

> If you tried it (in s2k), you would realize that you cannot use 3 part
> naming for creating procedures or functions. These statements are limited
> to accepting an owner name (optional) and an object name.
> Try the following statement:
> create procedure pubs.dbo.junk as select getdate()
new

CREATE via Dynamic SQL into new database?

From a stored procedure running in the context of one database, I would like
to create a set of objects (stored procedures, functions, views, users) into
a newly-created second database, where the name is dynamically determined.
Creating the new database and retrieving its name is no problem, the problem
is executing CREATE FUNCTION, CREATE PROCEDURE, etc. in the context of the
new database.
As you know, executing dynamic SQL 'use database' won't change the context
of an executing procedure. And 'use database; create function ...' doesn't
work, because the create statements need to be in their own batch. I cannot
store the objects in Master, so I can't have them automatically created with
the new database.
Is there a way to copy the objects from an existing (i.e. template) database
to the new one using dynamic SQL? Any way to attach a copy of a template
database file to a new database dynamically?
Or any out-of-the-box ideas?declare @.sql nvarchar(1000)
set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
exec sp_executesql @.sql
> From a stored procedure running in the context of one database, I would
> like to create a set of objects (stored procedures, functions, views,
> users) into a newly-created second database, where the name is dynamically
> determined. Creating the new database and retrieving its name is no
> problem, the problem is executing CREATE FUNCTION, CREATE PROCEDURE, etc.
> in the context of the new database.
> As you know, executing dynamic SQL 'use database' won't change the context
> of an executing procedure. And 'use database; create function ...' doesn't
> work, because the create statements need to be in their own batch. I
> cannot store the objects in Master, so I can't have them automatically
> created with the new database.
> Is there a way to copy the objects from an existing (i.e. template)
> database to the new one using dynamic SQL? Any way to attach a copy of a
> template database file to a new database dynamically?
> Or any out-of-the-box ideas?
--
new|||here's a real hum-dinger: (this is all on one line)
exec opendatasource('sqloledb', 'data
source=YourServer;uid=UserId;pwd=Password').YourDatabase.dbo.sp_executesql
N'create table mydatabase.dbo.newtable (myfield1 int)'
You'll want to change the following areas:
YourServer
UserId
Password
YourDatabase
... and the statement of course
> declare @.sql nvarchar(1000)
> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> exec sp_executesql @.sql
>
>> From a stored procedure running in the context of one database, I would
>> like to create a set of objects (stored procedures, functions, views,
>> users) into a newly-created second database, where the name is
>> dynamically determined. Creating the new database and retrieving its name
>> is no problem, the problem is executing CREATE FUNCTION, CREATE
>> PROCEDURE, etc. in the context of the new database.
>> As you know, executing dynamic SQL 'use database' won't change the
>> context of an executing procedure. And 'use database; create function
>> ...' doesn't work, because the create statements need to be in their own
>> batch. I cannot store the objects in Master, so I can't have them
>> automatically created with the new database.
>> Is there a way to copy the objects from an existing (i.e. template)
>> database to the new one using dynamic SQL? Any way to attach a copy of a
>> template database file to a new database dynamically?
>> Or any out-of-the-box ideas?
>
--
new|||Thanks, but this isn't the issue. Issue is that from a stored procedure (or
batch, for that matter) running in the context of database A, do:
declare @.DBName varchar(20)
set @.DBName = 'dynamic'
declare @.SQL varchar(200)
set @.SQL = 'use ' + @.DBName + '; create function foo ...'
exec (@.SQL)
Doesn't work because 'create function' must be at the beginning of a batch.
set @.SQL = 'create function ' + @.DBName + '.dbo.foo ...' doesn't work by
design.
Need to create functions, stored procs etc. in a different,
dynamically-determined database.
"beginthreadex" wrote:
> declare @.sql nvarchar(1000)
> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> exec sp_executesql @.sql
>
> > From a stored procedure running in the context of one database, I would
> > like to create a set of objects (stored procedures, functions, views,
> > users) into a newly-created second database, where the name is dynamically
> > determined. Creating the new database and retrieving its name is no
> > problem, the problem is executing CREATE FUNCTION, CREATE PROCEDURE, etc.
> > in the context of the new database.
> >
> > As you know, executing dynamic SQL 'use database' won't change the context
> > of an executing procedure. And 'use database; create function ...' doesn't
> > work, because the create statements need to be in their own batch. I
> > cannot store the objects in Master, so I can't have them automatically
> > created with the new database.
> >
> > Is there a way to copy the objects from an existing (i.e. template)
> > database to the new one using dynamic SQL? Any way to attach a copy of a
> > template database file to a new database dynamically?
> >
> > Or any out-of-the-box ideas?
> --
> new
>|||LOL ... next it will be sp_cmdshell(osql ... ). :-)
"beginthreadex" wrote:
> here's a real hum-dinger: (this is all on one line)
> exec opendatasource('sqloledb', 'data
> source=YourServer;uid=UserId;pwd=Password').YourDatabase.dbo.sp_executesql
> N'create table mydatabase.dbo.newtable (myfield1 int)'
> You'll want to change the following areas:
> YourServer
> UserId
> Password
> YourDatabase
> ... and the statement of course
>
> > declare @.sql nvarchar(1000)
> > set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> > exec sp_executesql @.sql
> >
> >
> >> From a stored procedure running in the context of one database, I would
> >> like to create a set of objects (stored procedures, functions, views,
> >> users) into a newly-created second database, where the name is
> >> dynamically determined. Creating the new database and retrieving its name
> >> is no problem, the problem is executing CREATE FUNCTION, CREATE
> >> PROCEDURE, etc. in the context of the new database.
> >>
> >> As you know, executing dynamic SQL 'use database' won't change the
> >> context of an executing procedure. And 'use database; create function
> >> ...' doesn't work, because the create statements need to be in their own
> >> batch. I cannot store the objects in Master, so I can't have them
> >> automatically created with the new database.
> >>
> >> Is there a way to copy the objects from an existing (i.e. template)
> >> database to the new one using dynamic SQL? Any way to attach a copy of a
> >> template database file to a new database dynamically?
> >>
> >> Or any out-of-the-box ideas?
> >
> --
> new
>|||The code I provided does execute the code in the other database. Hence, the
"mydatabase" reference. So, here's your code mixed with mine:
declare @.DBName varchar(20)
set @.DBName = 'dynamic'
declare @.SQL varchar(200)
set @.SQL = 'create function [' + @.DBName + '].dbo.foo ...'
exec sp_executesql @.sql
If there is something else that is confusing please let me know. Because I'm
referencing the database name this will run for the context of the other
database.
;)
> Thanks, but this isn't the issue. Issue is that from a stored procedure
> (or batch, for that matter) running in the context of database A, do:
> declare @.DBName varchar(20)
> set @.DBName = 'dynamic'
> declare @.SQL varchar(200)
> set @.SQL = 'use ' + @.DBName + '; create function foo ...'
> exec (@.SQL)
> Doesn't work because 'create function' must be at the beginning of a
> batch.
> set @.SQL = 'create function ' + @.DBName + '.dbo.foo ...' doesn't work by
> design.
> Need to create functions, stored procs etc. in a different,
> dynamically-determined database.
>
>> declare @.sql nvarchar(1000)
>> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
>> exec sp_executesql @.sql|||If you tried it (in s2k), you would realize that you cannot use 3 part
naming for creating procedures or functions. These statements are limited
to accepting an owner name (optional) and an object name.
Try the following statement:
create procedure pubs.dbo.junk as select getdate()|||Thanks for the response, I really do appreciate it. But CREATE no longer
accepts a DB name reference for functions/procedures - at least in SQL Server
2000.
"beginthreadex" wrote:
> The code I provided does execute the code in the other database. Hence, the
> "mydatabase" reference. So, here's your code mixed with mine:
> declare @.DBName varchar(20)
> set @.DBName = 'dynamic'
> declare @.SQL varchar(200)
> set @.SQL = 'create function [' + @.DBName + '].dbo.foo ...'
> exec sp_executesql @.sql
> If there is something else that is confusing please let me know. Because I'm
> referencing the database name this will run for the context of the other
> database.
> ;)
> > Thanks, but this isn't the issue. Issue is that from a stored procedure
> > (or batch, for that matter) running in the context of database A, do:
> >
> > declare @.DBName varchar(20)
> > set @.DBName = 'dynamic'
> > declare @.SQL varchar(200)
> > set @.SQL = 'use ' + @.DBName + '; create function foo ...'
> > exec (@.SQL)
> >
> > Doesn't work because 'create function' must be at the beginning of a
> > batch.
> >
> > set @.SQL = 'create function ' + @.DBName + '.dbo.foo ...' doesn't work by
> > design.
> >
> > Need to create functions, stored procs etc. in a different,
> > dynamically-determined database.
> >
> >
> >> declare @.sql nvarchar(1000)
> >> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> >> exec sp_executesql @.sql
>|||I deeply apologize! The "Create Table" code does allow for this.
However this DOES work as I have just tested:
exec opendatasource('sqloledb', 'data
source=MySource;uid=MyUID;pwd=MyPWD').pubs.dbo.sp_execsql N'create
procedure dbo.junk as select getdate()'
I know it's not the prettiest, but it DOES work.
> If you tried it (in s2k), you would realize that you cannot use 3 part
> naming for creating procedures or functions. These statements are limited
> to accepting an owner name (optional) and an object name.
> Try the following statement:
> create procedure pubs.dbo.junk as select getdate()
--
new|||Hello,
I suggest that you refer to the following web site:
http://www.databasejournal.com/features/mssql/article.php/3441031
You may try to use sp_MSforeachdb. I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
=====================================================When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================This posting is provided "AS IS" with no warranties, and confers no rights.

CREATE via Dynamic SQL into new database?

From a stored procedure running in the context of one database, I would like
to create a set of objects (stored procedures, functions, views, users) into
a newly-created second database, where the name is dynamically determined.
Creating the new database and retrieving its name is no problem, the problem
is executing CREATE FUNCTION, CREATE PROCEDURE, etc. in the context of the
new database.
As you know, executing dynamic SQL 'use database' won't change the context
of an executing procedure. And 'use database; create function ...' doesn't
work, because the create statements need to be in their own batch. I cannot
store the objects in Master, so I can't have them automatically created with
the new database.
Is there a way to copy the objects from an existing (i.e. template) database
to the new one using dynamic SQL? Any way to attach a copy of a template
database file to a new database dynamically?
Or any out-of-the-box ideas?
declare @.sql nvarchar(1000)
set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
exec sp_executesql @.sql

> From a stored procedure running in the context of one database, I would
> like to create a set of objects (stored procedures, functions, views,
> users) into a newly-created second database, where the name is dynamically
> determined. Creating the new database and retrieving its name is no
> problem, the problem is executing CREATE FUNCTION, CREATE PROCEDURE, etc.
> in the context of the new database.
> As you know, executing dynamic SQL 'use database' won't change the context
> of an executing procedure. And 'use database; create function ...' doesn't
> work, because the create statements need to be in their own batch. I
> cannot store the objects in Master, so I can't have them automatically
> created with the new database.
> Is there a way to copy the objects from an existing (i.e. template)
> database to the new one using dynamic SQL? Any way to attach a copy of a
> template database file to a new database dynamically?
> Or any out-of-the-box ideas?
new
|||here's a real hum-dinger: (this is all on one line)
exec opendatasource('sqloledb', 'data
source=YourServer;uid=UserId;pwd=Password').YourDa tabase.dbo.sp_executesql
N'create table mydatabase.dbo.newtable (myfield1 int)'
You'll want to change the following areas:
YourServer
UserId
Password
YourDatabase
... and the statement of course

> declare @.sql nvarchar(1000)
> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> exec sp_executesql @.sql
>
>
new
|||Thanks, but this isn't the issue. Issue is that from a stored procedure (or
batch, for that matter) running in the context of database A, do:
declare @.DBName varchar(20)
set @.DBName = 'dynamic'
declare @.SQL varchar(200)
set @.SQL = 'use ' + @.DBName + '; create function foo ...'
exec (@.SQL)
Doesn't work because 'create function' must be at the beginning of a batch.
set @.SQL = 'create function ' + @.DBName + '.dbo.foo ...' doesn't work by
design.
Need to create functions, stored procs etc. in a different,
dynamically-determined database.
"beginthreadex" wrote:

> declare @.sql nvarchar(1000)
> set @.sql = 'create table mydatabase.dbo.newtable (myfield1 int)'
> exec sp_executesql @.sql
>
> --
> new
>
|||LOL ... next it will be sp_cmdshell(osql ... ). :-)
"beginthreadex" wrote:

> here's a real hum-dinger: (this is all on one line)
> exec opendatasource('sqloledb', 'data
> source=YourServer;uid=UserId;pwd=Password').YourDa tabase.dbo.sp_executesql
> N'create table mydatabase.dbo.newtable (myfield1 int)'
> You'll want to change the following areas:
> YourServer
> UserId
> Password
> YourDatabase
> ... and the statement of course
>
> --
> new
>
|||The code I provided does execute the code in the other database. Hence, the
"mydatabase" reference. So, here's your code mixed with mine:
declare @.DBName varchar(20)
set @.DBName = 'dynamic'
declare @.SQL varchar(200)
set @.SQL = 'create function [' + @.DBName + '].dbo.foo ...'
exec sp_executesql @.sql
If there is something else that is confusing please let me know. Because I'm
referencing the database name this will run for the context of the other
database.
;)
[vbcol=seagreen]
> Thanks, but this isn't the issue. Issue is that from a stored procedure
> (or batch, for that matter) running in the context of database A, do:
> declare @.DBName varchar(20)
> set @.DBName = 'dynamic'
> declare @.SQL varchar(200)
> set @.SQL = 'use ' + @.DBName + '; create function foo ...'
> exec (@.SQL)
> Doesn't work because 'create function' must be at the beginning of a
> batch.
> set @.SQL = 'create function ' + @.DBName + '.dbo.foo ...' doesn't work by
> design.
> Need to create functions, stored procs etc. in a different,
> dynamically-determined database.
>
|||If you tried it (in s2k), you would realize that you cannot use 3 part
naming for creating procedures or functions. These statements are limited
to accepting an owner name (optional) and an object name.
Try the following statement:
create procedure pubs.dbo.junk as select getdate()
|||I deeply apologize! The "Create Table" code does allow for this.
However this DOES work as I have just tested:
exec opendatasource('sqloledb', 'data
source=MySource;uid=MyUID;pwd=MyPWD').pubs.dbo.sp_ execsql N'create
procedure dbo.junk as select getdate()'
I know it's not the prettiest, but it DOES work.

> If you tried it (in s2k), you would realize that you cannot use 3 part
> naming for creating procedures or functions. These statements are limited
> to accepting an owner name (optional) and an object name.
> Try the following statement:
> create procedure pubs.dbo.junk as select getdate()
new