Showing posts with label views. Show all posts
Showing posts with label views. Show all posts

Saturday, February 25, 2012

Creating a database with a GUI

Is there any tools to create a database with tables, views, and all that jazz with a GUI. I went through the exammle on beta.asp.net but the way they created their db was with a script. I want to create everything with a GUI app. If this gui app doesn't exist can someone point me to where I can find more info about creating db, tables etc with sql server 2005 express? Thanks in advance.
-Daticus

Try the link below and download the Express Manager. Hope this helps.
http://www.microsoft.com/sql/2005/productinfo/ctp.mspx#EBAA|||I think this is what your looking for...
Andrea Montanari
andrea.sql@.virgilio.it
http://www.asql.biz/DbaMgr.shtm
You can also use an Access Project to build an SQL database graphically
Good Luck
Simon

Friday, February 17, 2012

Create views only if database exists

Hi, I have a script that I execute, which checks whether a database exists, I
wish to then only create the views if this DB exists. I have been attempting
to use return, like this:
if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
CATALOG_NAME='velocity')
return
and i then have my create views after this, but it still executes the rest
of the script, (it seems to be due to the fact I have a GO commands after
each view i then try to create, otherwise if I remove the GO's sql says
'CREATE VIEW' must be the first statement in a query batch. Anyone have any
ideas?
Thanks,
DaveHow do you execute the script? OSQL etc will exit of you issue a RAISERROR with a state of 127.
Another alternative is to do a REISERROR with a high enough severity level (I believe 19 or higher
will do).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
> Hi, I have a script that I execute, which checks whether a database exists, I
> wish to then only create the views if this DB exists. I have been attempting
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have any
> ideas?
> Thanks,
> Dave|||Hi Tibor, thanks for the quick response.
I execute it as part of an installation (using Wise). Unfortunately at the
moment, it does raise an error as the script continues, so the person
installing the software sees an error. which is not really acceptable
"Tibor Karaszi" wrote:
> How do you execute the script? OSQL etc will exit of you issue a RAISERROR with a state of 127.
> Another alternative is to do a REISERROR with a high enough severity level (I believe 19 or higher
> will do).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Dave" <Dave@.discussions.microsoft.com> wrote in message
> news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
> > Hi, I have a script that I execute, which checks whether a database exists, I
> > wish to then only create the views if this DB exists. I have been attempting
> > to use return, like this:
> >
> > if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> > CATALOG_NAME='velocity')
> > return
> >
> > and i then have my create views after this, but it still executes the rest
> > of the script, (it seems to be due to the fact I have a GO commands after
> > each view i then try to create, otherwise if I remove the GO's sql says
> > 'CREATE VIEW' must be the first statement in a query batch. Anyone have any
> > ideas?
> >
> > Thanks,
> > Dave
>|||Hi,
Use dynamic sql to build you sql statement to create your views.
Ray
"Dave" wrote:
> Hi, I have a script that I execute, which checks whether a database exists, I
> wish to then only create the views if this DB exists. I have been attempting
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have any
> ideas?
> Thanks,
> Dave|||Hi Ray,
Can you please elaborate, not sure what you mean.
Dave
"rb" wrote:
> Hi,
> Use dynamic sql to build you sql statement to create your views.
> Ray
> "Dave" wrote:
> > Hi, I have a script that I execute, which checks whether a database exists, I
> > wish to then only create the views if this DB exists. I have been attempting
> > to use return, like this:
> >
> > if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> > CATALOG_NAME='velocity')
> > return
> >
> > and i then have my create views after this, but it still executes the rest
> > of the script, (it seems to be due to the fact I have a GO commands after
> > each view i then try to create, otherwise if I remove the GO's sql says
> > 'CREATE VIEW' must be the first statement in a query batch. Anyone have any
> > ideas?
> >
> > Thanks,
> > Dave|||Don't use INFORMATION_SCHEMA.SCHEMATA to determine database existence or
enumerate databases. Although this view will provide a list of databases in
SQL 2000, that behavior doesn't conform to the ANSI standard. The
behavior was changed in SQL 2005 to list only schema in the current
database. To check for database existence, consider using IF
DB_ID('velocity') IS NOT NULL.
Regarding your original question, I can't help with Wise specifically but
most installers provide a way to conditionally execute installation tasks.
If you can't figure that out, you can execute a command file containing the
conditional code from the installer. For example (text may wrap):
REM Execute CreateViews.sql conditionally
@.OSQL -E -i CheckDatabase.sql
@.IF %ERRORLEVEL% == 0 OSQL -E -i CreateViews.sql
--CheckDatabase.sql
EXIT(SELECT CASE WHEN DB_ID('velocity') IS NULL THEN 1 ELSE 0 END)
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
> Hi, I have a script that I execute, which checks whether a database
> exists, I
> wish to then only create the views if this DB exists. I have been
> attempting
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have
> any
> ideas?
> Thanks,
> Dave|||Hi Dave,
You can build a dynamic sql statement and then execute it. I hope this
example helps:
IF EXISTS(Select 1 from information_schema.schemata Where catalog_name ='Test2')
BEGIN
DECLARE @.sql nvarchar(200)
Set @.sql = 'CREATE VIEW dbo.vwt1 as Select * from test2.dbo.sysobjects'
EXEC(@.sql)
Set @.sql = 'CREATE VIEW dbo.vwt2 as Select * from test2.dbo.sysindexes'
EXEC(@.sql)
END
"Dave" wrote:
> Hi Ray,
> Can you please elaborate, not sure what you mean.
> Dave
> "rb" wrote:
> > Hi,
> >
> > Use dynamic sql to build you sql statement to create your views.
> >
> > Ray
> >
> > "Dave" wrote:
> >
> > > Hi, I have a script that I execute, which checks whether a database exists, I
> > > wish to then only create the views if this DB exists. I have been attempting
> > > to use return, like this:
> > >
> > > if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> > > CATALOG_NAME='velocity')
> > > return
> > >
> > > and i then have my create views after this, but it still executes the rest
> > > of the script, (it seems to be due to the fact I have a GO commands after
> > > each view i then try to create, otherwise if I remove the GO's sql says
> > > 'CREATE VIEW' must be the first statement in a query batch. Anyone have any
> > > ideas?
> > >
> > > Thanks,
> > > Dave

Create views only if database exists

How do you execute the script? OSQL etc will exit of you issue a RAISERROR w
ith a state of 127.
Another alternative is to do a REISERROR with a high enough severity level (
I believe 19 or higher
will do).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
> Hi, I have a script that I execute, which checks whether a database exists
, I
> wish to then only create the views if this DB exists. I have been attempti
ng
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have an
y
> ideas?
> Thanks,
> DaveHi Tibor, thanks for the quick response.
I execute it as part of an installation (using Wise). Unfortunately at the
moment, it does raise an error as the script continues, so the person
installing the software sees an error. which is not really acceptable
"Tibor Karaszi" wrote:

> How do you execute the script? OSQL etc will exit of you issue a RAISERROR
with a state of 127.
> Another alternative is to do a REISERROR with a high enough severity level
(I believe 19 or higher
> will do).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Dave" <Dave@.discussions.microsoft.com> wrote in message
> news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
>|||Hi,
Use dynamic sql to build you sql statement to create your views.
Ray
"Dave" wrote:

> Hi, I have a script that I execute, which checks whether a database exists
, I
> wish to then only create the views if this DB exists. I have been attempti
ng
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have an
y
> ideas?
> Thanks,
> Dave|||Hi Ray,
Can you please elaborate, not sure what you mean.
Dave
"rb" wrote:
[vbcol=seagreen]
> Hi,
> Use dynamic sql to build you sql statement to create your views.
> Ray
> "Dave" wrote:
>|||Hi, I have a script that I execute, which checks whether a database exists,
I
wish to then only create the views if this DB exists. I have been attempting
to use return, like this:
if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
CATALOG_NAME='velocity')
return
and i then have my create views after this, but it still executes the rest
of the script, (it seems to be due to the fact I have a GO commands after
each view i then try to create, otherwise if I remove the GO's sql says
'CREATE VIEW' must be the first statement in a query batch. Anyone have any
ideas?
Thanks,
Dave|||How do you execute the script? OSQL etc will exit of you issue a RAISERROR w
ith a state of 127.
Another alternative is to do a REISERROR with a high enough severity level (
I believe 19 or higher
will do).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
> Hi, I have a script that I execute, which checks whether a database exists
, I
> wish to then only create the views if this DB exists. I have been attempti
ng
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have an
y
> ideas?
> Thanks,
> Dave|||Hi Tibor, thanks for the quick response.
I execute it as part of an installation (using Wise). Unfortunately at the
moment, it does raise an error as the script continues, so the person
installing the software sees an error. which is not really acceptable
"Tibor Karaszi" wrote:

> How do you execute the script? OSQL etc will exit of you issue a RAISERROR
with a state of 127.
> Another alternative is to do a REISERROR with a high enough severity level
(I believe 19 or higher
> will do).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Dave" <Dave@.discussions.microsoft.com> wrote in message
> news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
>|||Hi,
Use dynamic sql to build you sql statement to create your views.
Ray
"Dave" wrote:

> Hi, I have a script that I execute, which checks whether a database exists
, I
> wish to then only create the views if this DB exists. I have been attempti
ng
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have an
y
> ideas?
> Thanks,
> Dave|||Hi Ray,
Can you please elaborate, not sure what you mean.
Dave
"rb" wrote:
[vbcol=seagreen]
> Hi,
> Use dynamic sql to build you sql statement to create your views.
> Ray
> "Dave" wrote:
>|||Don't use INFORMATION_SCHEMA.SCHEMATA to determine database existence or
enumerate databases. Although this view will provide a list of databases in
SQL 2000, that behavior doesn't conform to the ANSI standard. The
behavior was changed in SQL 2005 to list only schema in the current
database. To check for database existence, consider using IF
DB_ID('velocity') IS NOT NULL.
Regarding your original question, I can't help with Wise specifically but
most installers provide a way to conditionally execute installation tasks.
If you can't figure that out, you can execute a command file containing the
conditional code from the installer. For example (text may wrap):
REM Execute CreateViews.sql conditionally
@.OSQL -E -i CheckDatabase.sql
@.IF %ERRORLEVEL% == 0 OSQL -E -i CreateViews.sql
--CheckDatabase.sql
EXIT(SELECT CASE WHEN DB_ID('velocity') IS NULL THEN 1 ELSE 0 END)
Hope this helps.
Dan Guzman
SQL Server MVP
"Dave" <Dave@.discussions.microsoft.com> wrote in message
news:F4BBCA1B-0296-43C8-8E42-CCCAB69044CA@.microsoft.com...
> Hi, I have a script that I execute, which checks whether a database
> exists, I
> wish to then only create the views if this DB exists. I have been
> attempting
> to use return, like this:
> if not exists(SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE
> CATALOG_NAME='velocity')
> return
> and i then have my create views after this, but it still executes the rest
> of the script, (it seems to be due to the fact I have a GO commands after
> each view i then try to create, otherwise if I remove the GO's sql says
> 'CREATE VIEW' must be the first statement in a query batch. Anyone have
> any
> ideas?
> Thanks,
> Dave

Create Views not Tables

I want to allow a group of users to create views but not be able to create new tables or stored procedures... how can I do this ??
Thanks, John :eek:See the GRANT (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ga-gz_8odw.asp) command in Transact-SQL.

-PatP

create views in the order of their dependency

In SQL 2K...

i need to write a script that writes a script that creates all views in the order that they are nested and I am feeling lazy. Does anyone have anything handy?

For example if View_BOB says...

CREATE VIEW VIEW_BOB
AS
SELECT COL_1 FROM VIEW_JOHN

I need the script to generate a script that creates View_JOHN before View_BOB.You can use Enterprise Manager to generate the script for you and it will take care of dependency of the objects.

SQL Server Helper
http://www.sql-server-helper.com

create views in stored procedures?

Can you create views within a stored procedure?

The short answer is 'Yes'; what exactly do you have in mind because this might not be such a good idea. You might need to use dynamic SQL to get the job done; otherwise, you might get a procedure compile error such as:

Server: Msg 156, Level 15, State 1, Procedure wacko, Line 6
Incorrect syntax near the keyword 'view'.

|||

I've got a report (Access front end) that takes over 2 minutes to run. Access sometimes times out before the report finishes. (I've already adjusted Access's timeout range to the maximum amount.) The report data is based on a view that pulls data from several other views and tables (using unions, outer joins, etc.). I decided the best way to keep the report from timing out in Access was to have the result set queried each night and inserted into a table. The report will now use the table as it's data source. The data doesn't have to be dymnamic as long as it's updated nightly. So, now I'm trying to create a stored procedure that will run nightly, inserting the final results into a table. I'm getting this error message for every line I'm trying to create a view on:

Server: Msg 156, Level 15, State 1, Procedure sp_insert_edirecalltbl, Line 16
Incorrect syntax near the keyword 'VIEW'.

Here's a small portion of the code in my procedure:

BEGIN
CREATE VIEW edirecallview
As SELECT top 100 xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
FROM edirecxmlrawview x FULL OUTER JOIN edirecrawdepview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace
END

BEGIN
select xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
into edirecalltbl
FROM edirecxmlrawview x FULL OUTER JOIN edirecrawdepview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace
END

Maybe I should use temporary tables instead of views? If I have to, I can use permanant tables instead of views. Ideas?

|||

You can use a permanent table to hold the summary information that you populate each night. The procedure can look like this

CREATE PROCEDURE spxyz

AS

BEGIN

Truncate mySummaryTable;

--<Drop indexes if the data is in millions, otherwise not>

INSERT INTO mySummaryTable(col1, col2, col3)

select a.col1, a.col2, b.col3

from TableA a inner join TableB b on a.pk = b.pk

--<Recreate indexes if you dropped before the insert>

--Thats it

END

|||

I would actually use a CTE (common table expression) instead of a view. This is not available in 2000 just 2005.

WITH cte_edirecallview (xmltrace, xmllname, xmlvetcode, xmltransdate, xmltotalamt, xrawtrace, xrawtransdate, xrawamount, deptrace, deptransdate, depamount, depvouchernum, drawtrace, drawtransdate, drawamount)

AS

(

SELECT top 100 xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
FROM edirecxmlrawview x FULL OUTER JOIN edirecrawdepview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace

)

select xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
into edirecalltbl
FROM cte_edirecallview x FULL OUTER JOIN cte_edirecallview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace

|||That would be so perfect...if only I was using 2005! Thanks, anyway. It's something to look forward to.|||

You can dynamically create the view via Exec('view_definition').

e.g.

Code Snippet

Exec ('create view edirectcalview AS

SELECT top 100 xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
FROM edirecxmlrawview x FULL OUTER JOIN edirecrawdepview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace')

However, in your case, you can just derive the query.

e.g.

Code Snippet

select xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
into edirecalltbl
FROM (

SELECT top 100 xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
FROM edirecxmlrawview x FULL OUTER JOIN edirecrawdepview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace)

x FULL OUTER JOIN (

SELECT top 100 xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
FROM edirecxmlrawview x FULL OUTER JOIN edirecrawdepview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace)

d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace

|||

In addition, in 2000 it was very typical to just give in an use a temporary table for this sort of thing, and to be honest, sometimes it can be better anyhow. It really depends on how well SQL Server happens to optimize your derived table. Sometimes it can manifest itself as multiple queries rather than spooling the rows into some temporary storage and calculating it once:

SELECT top 100 xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount

into #edirecallview
FROM edirecxmlrawview x FULL OUTER JOIN edirecrawdepview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace

select xmltrace,
xmllname,
xmlvetcode,
xmltransdate,
xmltotalamt,
x.rawtrace as xrawtrace,
x.rawtransdate as xrawtransdate,
x.rawamount as xrawamount,
deptrace,
deptransdate,
depamount,
depvouchernum,
d.rawtrace as drawtrace,
d.rawtransdate as drawtransdate,
d.rawamount as drawamount
into edirecalltbl
FROM #edirecallview x FULL OUTER JOIN #edirecallview d
on x.rawtrace = d.rawtrace or
xmltrace = deptrace
order by xmltrace, x.rawtrace, deptrace

This would be one of the steps I might take to optimize your query if it were running slowly (and we had exhausted all plan based optimizations) anyhow.

Tuesday, February 14, 2012

Create view from cursor

I have multiple locations that I want to create views for each
individual location.

I am using a cursor to create the views for each location. So, the
cursor grabs site #1 then <should> create view_site_#1, then grab site
#2 and <should> create view_site_#2.

For some reason it doesn't like the view name with the @.site in it.
Any ideas of how to get this done?

Here's the cursor...

declare @.site varchar(5)

declare c_site cursor for
select station from VHAISLCAUDIA.VISN_SITE
order by station

open c_site
fetch from c_site
into @.site

while (@.@.fetch_status = 0)
begin

CREATE VIEW Site_All_Data_+ @.site
AS
SELECT *
FROM dbo.[600_All_Suggested_Data]
WHERE (Site = @.site)

Print 'View for ' + @.site + ' Created'

fetch next from c_site into @.site
end
close c_site
deallocate c_site
return

endThis is actually one of the few times that a cursor and dynamic SQL can
be useful; this administrative scripting is a great target for this
sort of stuff.

Anyway, you need to use dynamic SQL for this:

DECLARE @.tSite TABLE (site varchar(5))
INSERT INTO @.tSite
SELECT 'ABCDE'
UNION ALL
SELECT 'FGHIJ'

declare @.site varchar(5)
DECLARE @.SQL nvarchar(2000)

declare c_site cursor for
select site from @.tsite

open c_site
fetch from c_site
into @.site

while (@.@.fetch_status = 0)
begin

SET @.SQL = 'CREATE VIEW Site_All_Data_' + @.site + '
AS
SELECT *
FROM dbo.[600_All_Suggested_Data]
WHERE Site = ''' + @.site + ''''

exec (@.SQL)

Print 'View for ' + @.site + ' Created'

fetch next from c_site into @.site
end
close c_site
deallocate c_site

HTH,
Stu|||Worked like a charm!

Thanks for helping a developer that forgets the 'simple' stuff
sometimes.

db55

CREATE VIEW - script to automate column names?

Hi,

I'm trying to create views on all my existing tables and for that I'd
like to create a script or so.
I don't want to specify the '*' for the columns in the create view
statement. I prefer to specify the column names.
I have the column names int sys.columns table but Do not know how to
handle them to have a statement like that:

CREATE VIEW myVIEW
WITH SCHEMABINDING
AS
SELECT col1name, col2name, col3name, etc...
from sys.columns
...???....

Anyone can help?

thx,

ChrisOn 12 Mar 2007 04:44:56 -0700, "clir" <christophe.leroquais@.gmail.com>
wrote:

Use a cursor to loop over the column names, all the while
concatenating a string variable. In the end, execute that string
(sp_executesql) and your view will be created.

-Tom.

Quote:

Originally Posted by

>Hi,
>
>I'm trying to create views on all my existing tables and for that I'd
>like to create a script or so.
>I don't want to specify the '*' for the columns in the create view
>statement. I prefer to specify the column names.
>I have the column names int sys.columns table but Do not know how to
>handle them to have a statement like that:
>
>CREATE VIEW myVIEW
>WITH SCHEMABINDING
>AS
SELECT col1name, col2name, col3name, etc...
from sys.columns
...???....
>
>
>Anyone can help?
>
>thx,
>
>Chris

Create View

Hi ,
If I create several views from the transaction table for reporting
purpose. Do the view occupy the disk space as well ?
Travis Tan
No. Unless it's an indexed view which contains data for the defined index.
Index views are sometimes used to speed up queries that require joins.
"Travis" <Travis@.discussions.microsoft.com> wrote in message
news:0FD3F4E1-DFC2-42AB-8E29-325438FFE11F@.microsoft.com...
> Hi ,
> If I create several views from the transaction table for reporting
> purpose. Do the view occupy the disk space as well ?
> --
> Travis Tan

Create View

Hi ,
If I create several views from the transaction table for reporting
purpose. Do the view occupy the disk space as well ?
Travis TanNo. Unless it's an indexed view which contains data for the defined index.
Index views are sometimes used to speed up queries that require joins.
"Travis" <Travis@.discussions.microsoft.com> wrote in message
news:0FD3F4E1-DFC2-42AB-8E29-325438FFE11F@.microsoft.com...
> Hi ,
> If I create several views from the transaction table for reporting
> purpose. Do the view occupy the disk space as well ?
> --
> Travis Tan

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