Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Friday, March 30, 2012

OpenQuery with join

Hi

I have a SP which queries a linked server using OpenQuery function.
The remote query includes a join and an IN clause to get the desire result. ( The linked server uses Transoft ODBC driver.)

The qry looks something like this:

select * from OpenQuery(SERVER1,
'select
distinct C.item, C.operation, C.STD_OPERATION,
D.operation_desc , C.operation_desc operation_desc
from TABLE1 C left join
(select distinct operation, operation_desc from TABLE1
where operation in (select distinct STD_OPERATION from TABLE1 where
item = ''9999999'' AND STD_OPERATION <> 0)
AND item = ''STANDARD'') D
on D.operation = C.STD_OPERATION where C.item = ''9999999'' ')

When I run this Qry I get the following error:

Server: Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing a query for execution against OLE DB provider 'MSDASQL'.
[OLE/DB provider returned message: [Transoft][TSODBC][usqlsd]')' expected here (DISTINCT)]

Any help would be greatly appreciated.
thxIs the problem that Transoft can't handle the distinct keyword? It is SQL-92 compliant, but maybe the driver can't handle it? Have you tried removing distinct and running the query again?

If this is the problem, you should be able to work around the problem using a group by clause.

Hth.

Paul Barbin

OPENQUERY vs. sp_executesql which is better?

I have recently seen the OPENQUERY function in a stored procedure which
was used to INSERT values into a table on a remote server. However, I
normally use the sp_executesql stored proc.
Can any one shed some light on which method is better and in what
cases?
The following code is from Microsoft website, not my own.
http://msdn.microsoft.com/library/d...br />
5xix.asp
<MS CODE>
EXEC sp_addlinkedserver 'OracleSvr',
'Oracle 7.3',
'MSDAORA',
'ORCLDB'
GO
SELECT *
FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM joe.titles')
GO
</MS CODE>
VS.
<MYCODE>
sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'
-- OR
OracleSvr..sp_executesql N'SELECT name, id FROM dbname.joe.titles'
</MYCODE>
Thanks for your time folks.
Johnny DWhy would you even consider sp_executesql in your case? There is no need to
use dynamic sql to access a linked server.
Andrew J. Kelly SQL MVP
"Johnny D" <john.dacosta@.gmail.com> wrote in message
news:1148649898.280530.174390@.j73g2000cwa.googlegroups.com...
>I have recently seen the OPENQUERY function in a stored procedure which
> was used to INSERT values into a table on a remote server. However, I
> normally use the sp_executesql stored proc.
> Can any one shed some light on which method is better and in what
> cases?
> The following code is from Microsoft website, not my own.
> http://msdn.microsoft.com/library/d... />
z_5xix.asp
> <MS CODE>
> EXEC sp_addlinkedserver 'OracleSvr',
> 'Oracle 7.3',
> 'MSDAORA',
> 'ORCLDB'
> GO
>
> SELECT *
> FROM OPENQUERY(OracleSvr, 'SELECT name, id FROM joe.titles')
> GO
> </MS CODE>
> VS.
> <MYCODE>
> sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'
> -- OR
> OracleSvr..sp_executesql N'SELECT name, id FROM dbname.joe.titles'
> </MYCODE>
>
> Thanks for your time folks.
>
> Johnny D
>|||Sorry Andrew, this was an oversight in me writing a simple query to
illustrate my question...
The reason for this is I would use a cursor to loop through my
different servernames
DECLARE @.rc INT
DECLARE @.v_sql NVARCHAR(4000)
DECLARE @.vc_servername VARCHAR(100)
DECLARE c_myservers CURSOR
FOR SELECT
servername
FROM listservers
WHERE active=1
FOR READ ONLY
OPEN c_myservers
FETCH NEXT FROM c_myservers INTO @.vc_servername
WHILE (@.@.FETCHSTATUS = 0 )
BEGIN
SET @.v_sql = N'SELECT name, id FROM ['+@.vc_servername +
'].dbname.joe.titles'
EXEC @.rc = sp_executesql @.v_sql
-- if @.rc <> 0
-- etc...
FETCH NEXT FROM c_myservers INTO @.vc_servername
END
CLOSE c_myservers
DEALLOCATE c_myservers
sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'|||Well one key point of using OPENQUERY is that it passes the statement to the
other server where it is executed as is. That way the remote server can
choose the proper plan for that statement without regard to what the rest of
the statement is that was issued locally. Kind of hard to explain but it is
truly a pass-through query where as a linked server query may be influenced
by the rest of the statement. For instance a join to a local table.
Andrew J. Kelly SQL MVP
"Johnny D" <john.dacosta@.gmail.com> wrote in message
news:1148652836.663893.44660@.g10g2000cwb.googlegroups.com...
> Sorry Andrew, this was an oversight in me writing a simple query to
> illustrate my question...
> The reason for this is I would use a cursor to loop through my
> different servernames
> DECLARE @.rc INT
> DECLARE @.v_sql NVARCHAR(4000)
> DECLARE @.vc_servername VARCHAR(100)
> DECLARE c_myservers CURSOR
> FOR SELECT
> servername
> FROM listservers
> WHERE active=1
> FOR READ ONLY
> OPEN c_myservers
> FETCH NEXT FROM c_myservers INTO @.vc_servername
> WHILE (@.@.FETCHSTATUS = 0 )
> BEGIN
> SET @.v_sql = N'SELECT name, id FROM ['+@.vc_servername +
> '].dbname.joe.titles'
> EXEC @.rc = sp_executesql @.v_sql
> -- if @.rc <> 0
> -- etc...
> FETCH NEXT FROM c_myservers INTO @.vc_servername
> END
> CLOSE c_myservers
> DEALLOCATE c_myservers
>
> sp_executesql N'SELECT name, id FROM OracleSvr.dbname.joe.titles'
>|||Johnny D (john.dacosta@.gmail.com) writes:
> I have recently seen the OPENQUERY function in a stored procedure which
> was used to INSERT values into a table on a remote server. However, I
> normally use the sp_executesql stored proc.
> Can any one shed some light on which method is better and in what
> cases?
I think they are as comparable as apples and oranges.
Apparently you loop over servers. That is nothing OPENQUERY can help
you with - the server name must be a constant.
Rather your choice is between:
SELECT @.sql = 'SELECT ... FROM ' + @.server + 'catalog.schema.tbl'
and
SELECT @.sql = 'SELECT ... FROM OPENQUERY(' + @.server + ', ' +
'''SELECT ... FROM catalog.schema.tbl'')'
That is, accessing the table in four-partnotation, or running a
passthrough query.

><MYCODE>
> OracleSvr..sp_executesql N'SELECT name, id FROM dbname.joe.titles'
></MYCODE>
Ehum, I don't think you will find sp_executesql on Oracle...
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Openquery syntax for function

There is a very complex query where I'm trying to call a function from a
linked server with input paramaters from the local database. This is the
general idea:
select openquery( linkserver, 'database.dbo.function( t1.c1, t2.c2 )'
from table1 t1
and table t2
where ...
I get a syntax error that doesn't recognize t1 and t2. How should I fix thi
s?
Thanks,Openquery() (i.e. ad-hoc/pass through function) only takes literal strings.
So, it's not possible to pass in any parameters.
Also, it's not possible to call a remote user-defined function in sqlserver
(i.e. srv.db.dbo.udf() is not allowed). So, you would have to create the
function locally.
-oj
"Lisa" <Lisa@.discussions.microsoft.com> wrote in message
news:B9DE16A3-1346-424B-81C7-AD161EC8A848@.microsoft.com...
> There is a very complex query where I'm trying to call a function from a
> linked server with input paramaters from the local database. This is the
> general idea:
> select openquery( linkserver, 'database.dbo.function( t1.c1, t2.c2 )'
> from table1 t1
> and table t2
> where ...
> I get a syntax error that doesn't recognize t1 and t2. How should I fix
> this?
> Thanks,|||Lisa
Is that Scalar UDF? Is that Inline Table-Valued UDF? Is that Multi-Statement
Table-Valued UDF?
Look at this technique written by Itzik Ben-Gan
CREATE FUNCTION dbo.fn_getinvid1() RETURNS int
AS
BEGIN
RETURN(SELECT newinvid FROM OPENQUERY([server_name],
'SET NOCOUNT ON; DECLARE @.invid AS INT;
UPDATE tempdb..Seq SET @.invid = val = val + 1; COMMIT;
SELECT @.invid AS newinvid;') AS O)
END
CREATE FUNCTION dbo.fn_getinvid2() RETURNS int
AS
BEGIN
RETURN(
SELECT newinvid
FROM OPENQUERY(
[server_name],
'SET NOCOUNT ON;
INSERT INTO tempdb..Seq2 DEFAULT VALUES
ROLLBACK;
SELECT SCOPE_IDENTITY() AS newinvid;') AS O)
END
"Lisa" <Lisa@.discussions.microsoft.com> wrote in message
news:B9DE16A3-1346-424B-81C7-AD161EC8A848@.microsoft.com...
> There is a very complex query where I'm trying to call a function from a
> linked server with input paramaters from the local database. This is the
> general idea:
> select openquery( linkserver, 'database.dbo.function( t1.c1, t2.c2 )'
> from table1 t1
> and table t2
> where ...
> I get a syntax error that doesn't recognize t1 and t2. How should I fix
> this?
> Thanks,|||I'm looking at more in line with your first function. but I have two
parameters (one an integer) and one a date that is being to the function tha
t
I want to use in the openquery statment. I can't get the sql right for it
though.
"Uri Dimant" wrote:

> Lisa
> Is that Scalar UDF? Is that Inline Table-Valued UDF? Is that Multi-Stateme
nt
> Table-Valued UDF?
> Look at this technique written by Itzik Ben-Gan
> CREATE FUNCTION dbo.fn_getinvid1() RETURNS int
> AS
> BEGIN
> RETURN(SELECT newinvid FROM OPENQUERY([server_name],
> 'SET NOCOUNT ON; DECLARE @.invid AS INT;
> UPDATE tempdb..Seq SET @.invid = val = val + 1; COMMIT;
> SELECT @.invid AS newinvid;') AS O)
> END
> CREATE FUNCTION dbo.fn_getinvid2() RETURNS int
> AS
> BEGIN
> RETURN(
> SELECT newinvid
> FROM OPENQUERY(
> [server_name],
> 'SET NOCOUNT ON;
> INSERT INTO tempdb..Seq2 DEFAULT VALUES
> ROLLBACK;
> SELECT SCOPE_IDENTITY() AS newinvid;') AS O)
> END
> "Lisa" <Lisa@.discussions.microsoft.com> wrote in message
> news:B9DE16A3-1346-424B-81C7-AD161EC8A848@.microsoft.com...
>
>

OpenQuery Problems with Date Function Different MS-SQL 7 / MS-SQL

Hi
It is a change in the Microsoft Oracle driver to support the newer versions
of Oracle. In effect, it is a change required by Oracle that Microsoft had t
o
comply with so that the driver would continue to work. AFAIK, the change was
introduced in MDAC v2.5
Regards
Mike
"Klaus" wrote:

> hi,
>
> Currently we move a couple of SQL Server 7.0 to SQL Server 2000 and right
> now we habe big problems with a OPENQUERY Statement
> The SQL Server 7.0 and Windows 2000 is a german version, the MS-SQL Server
> 2000 and Windows 2000 is in english
>
> The Query on the MS-SQL Server 7 works fine
> SELECT * FROM OPENQUERY (ABKG002,
> 'SELECT DISTINCT NODP.AU_AUD_BENUTZER AS BA_AUD_ERST_VON FROM
> V3FADM.AUODP_ORDERPOS_OTAB NODP WHERE NODP.AU_AUD_ZEITPUNKT >
> ''01.03.2005''')
>
> With the same Query under MS-SQL 2000 is the result
> Server: Msg 7330, Level 16, State 2, Line 1
> Could not fetch a row from OLE DB provider 'MSDAORA'.
> [OLE/DB provider returned message: ORA-01843: not a valid month]
> OLE DB error trace [OLE/DB Provider 'MSDAORA' IRowset::GetNextRows returne
d
> 0x80040e07].
>
>
> if we change the Query like below, the OPENQUERY is fine. IS THIS REALLY
> only the way the OPENQUERY works fine? That means for us we have to recrea
te
> a lot of VB Applications
> SELECT * FROM OPENQUERY (ABKG002, 'SELECT DISTINCT NODP.AU_AUD_BENUTZER AS
> BA_AUD_ERST_VON FROM V3FADM.AUODP_ORDERPOS_OTAB NODP WHERE
> NODP.AU_AUD_ZEITPUNKT > to_date(''22.03.2005'',''dd.mm.yyyy'')')
>
>
> Has anybody an IDEA?
>
>
> thx
> klaus
>
>
>Hi Mike
thanks for the very fast and good answer !
Klaus
"Mike Epprecht (SQL MVP)" <mike@.epprecht.net> schrieb im Newsbeitrag
news:5C2EC99E-CB18-4B1D-8DCA-6A04ED22255C@.microsoft.com...
> Hi
> It is a change in the Microsoft Oracle driver to support the newer
> versions
> of Oracle. In effect, it is a change required by Oracle that Microsoft had
> to
> comply with so that the driver would continue to work. AFAIK, the change
> was
> introduced in MDAC v2.5
> Regards
> Mike
> "Klaus" wrote:
>

Friday, March 9, 2012

Open a script file for a script

Hi,
Is it possible to open a sql file a run the content from another file?
MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
refman/5.0/en/batch-commands.html).
I saw this news
[url]http://groups.google.com.au/group/microsoft.public.sqlserver.server/browse_thread/thread/18aa1a599d5e4ec/7ea0f09197e729ee?lnk=gst&q=run+file+script&rnum=3& hl=en#7ea0f09197e729ee[/url]
but it is not exactly what I want to do.
Let me know if it's possible.
Thank you.
Ben
What tool are you using to run the script? Also, does the script file exist on the server machine or
the client machine?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179729481.030659.246610@.n15g2000prd.googlegr oups.com...
> Hi,
> Is it possible to open a sql file a run the content from another file?
> MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
> refman/5.0/en/batch-commands.html).
> I saw this news
> [url]http://groups.google.com.au/group/microsoft.public.sqlserver.server/browse_thread/thread/18aa1a599d5e4ec/7ea0f09197e729ee?lnk=gst&q=run+file+script&rnum=3& hl=en#7ea0f09197e729ee[/url]
> but it is not exactly what I want to do.
> Let me know if it's possible.
> Thank you.
> Ben
>
|||On May 21, 4:47 pm, "Tibor Karaszi"
<tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:[vbcol=seagreen]
> What tool are you using to run the script? Also, does the script file exist on the server machine or
> the client machine?
> --
> Tibor Karaszi, SQL Server MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/tibor_karaszi
> <benjamin.am...@.gmail.com> wrote in message
> news:1179729481.030659.246610@.n15g2000prd.googlegr oups.com...
>
>
>
I'm using the SQL Server Management Studio.
The file will be run on the client machine.
The reason why I'm asking for this function is because we have 300+
SQL script files that need to be ran in a specific order. Opening them
1 by 1 and running them using the manager is *really* time consuming.
The final script that will run all the others will be used by my
client as a unique "run all" script.
Thank you for your answer.
Ben
|||You can run SSMS in "SQLCMD mode" (the Query menu) or use SQLCMD.EXE. If you do this, you can use :r
to have the tool to read a file. The file cannot contain GO (batch separator), at lest this was a
restriction last time I tested (2000).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179731210.026500.253250@.r3g2000prh.googlegro ups.com...
> On May 21, 4:47 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> I'm using the SQL Server Management Studio.
> The file will be run on the client machine.
> The reason why I'm asking for this function is because we have 300+
> SQL script files that need to be ran in a specific order. Opening them
> 1 by 1 and running them using the manager is *really* time consuming.
> The final script that will run all the others will be used by my
> client as a unique "run all" script.
> Thank you for your answer.
> Ben
>
|||Since you need to run the 300+ files in a certain order, I would use a
simple vbscript and SMO to do the executes. Use the FileSystemObject to
open each file in the correct order (dos dir /b command output to file will
get you the list of files, which you can then order as appropriate), and
execute against a smo connection to the appropriate server/db.
TheSQLGuru
President
Indicium Resources, Inc.
<benjamin.amiot@.gmail.com> wrote in message
news:1179731210.026500.253250@.r3g2000prh.googlegro ups.com...
> On May 21, 4:47 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> I'm using the SQL Server Management Studio.
> The file will be run on the client machine.
> The reason why I'm asking for this function is because we have 300+
> SQL script files that need to be ran in a specific order. Opening them
> 1 by 1 and running them using the manager is *really* time consuming.
> The final script that will run all the others will be used by my
> client as a unique "run all" script.
> Thank you for your answer.
> Ben
>
|||On May 22, 4:30 am, "TheSQLGuru" <kgbo...@.earthlink.net> wrote:[vbcol=seagreen]
> Since you need to run the 300+ files in a certain order, I would use a
> simple vbscript and SMO to do the executes. Use the FileSystemObject to
> open each file in the correct order (dos dir /b command output to file will
> get you the list of files, which you can then order as appropriate), and
> execute against a smo connection to the appropriate server/db.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> <benjamin.am...@.gmail.com> wrote in message
> news:1179731210.026500.253250@.r3g2000prh.googlegro ups.com...
>
>
>
>
>
>
>
>
Ok,
I'll try both method and see which one is the easiest to implement.
Thank you for your messages.
Ben
|||On May 21, 6:43 pm, "Tibor Karaszi"
<tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:[vbcol=seagreen]
> You can run SSMS in "SQLCMD mode" (the Query menu) or use SQLCMD.EXE. If you do this, you can use :r
> to have the tool to read a file. The file cannot contain GO (batch separator), at lest this was a
> restriction last time I tested (2000).
> --
> Tibor Karaszi, SQL Server MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/tibor_karaszi
> <benjamin.am...@.gmail.com> wrote in message
> news:1179731210.026500.253250@.r3g2000prh.googlegro ups.com...
>
>
>
>
>
>
>
>
Hi Tibor,
Thanks for your solution, but I won't be able to us it as most of the
files are using GO in them. I can't remove this command as the scripts
are generated by different teams in the company.
I'll try the solution of TheSQLGuru.
Ben
|||> Thanks for your solution, but I won't be able to us it as most of the
> files are using GO in them. I can't remove this command as the scripts
> are generated by different teams in the company.
This is one of the reasons to always read BOL. Reading current BOL, it seems like you *can* have
batch terminator in the inner script. Quotes from BOL:
"If the file contains Transact-SQL statements that arenot followed by GO, you must enter GO on the
line that follows :r."
"The file will be read and executed after a batch terminator is encountered. You can issue multiple
:r commands. The file may include any sqlcmd command. This includes the batch terminator GO."
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179790434.126680.210820@.r3g2000prh.googlegro ups.com...
> On May 21, 6:43 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> Hi Tibor,
> Thanks for your solution, but I won't be able to us it as most of the
> files are using GO in them. I can't remove this command as the scripts
> are generated by different teams in the company.
> I'll try the solution of TheSQLGuru.
> Ben
>

Open a script file for a script

Hi,
Is it possible to open a sql file a run the content from another file?
MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
refman/5.0/en/batch-commands.html).
I saw this news
http://groups.google.com.au/group/microsoft.public.sqlserver.server/browse_thread/thread/18aa1a599d5e4ec/7ea0f09197e729ee?lnk=gst&q=run+file+script&rnum=3&hl=en#7ea0f09197e729ee
but it is not exactly what I want to do.
Let me know if it's possible.
Thank you.
BenWhat tool are you using to run the script? Also, does the script file exist on the server machine or
the client machine?
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
> Hi,
> Is it possible to open a sql file a run the content from another file?
> MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
> refman/5.0/en/batch-commands.html).
> I saw this news
> http://groups.google.com.au/group/microsoft.public.sqlserver.server/browse_thread/thread/18aa1a599d5e4ec/7ea0f09197e729ee?lnk=gst&q=run+file+script&rnum=3&hl=en#7ea0f09197e729ee
> but it is not exactly what I want to do.
> Let me know if it's possible.
> Thank you.
> Ben
>|||On May 21, 4:47 pm, "Tibor Karaszi"
<tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> What tool are you using to run the script? Also, does the script file exist on the server machine or
> the client machine?
> --
> Tibor Karaszi, SQL Server MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/tibor_karaszi
> <benjamin.am...@.gmail.com> wrote in message
> news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
> > Hi,
> > Is it possible to open a sql file a run the content from another file?
> > MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
> > refman/5.0/en/batch-commands.html).
> > I saw this news
> >http://groups.google.com.au/group/microsoft.public.sqlserver.server/b...
> > but it is not exactly what I want to do.
> > Let me know if it's possible.
> > Thank you.
> > Ben
I'm using the SQL Server Management Studio.
The file will be run on the client machine.
The reason why I'm asking for this function is because we have 300+
SQL script files that need to be ran in a specific order. Opening them
1 by 1 and running them using the manager is *really* time consuming.
The final script that will run all the others will be used by my
client as a unique "run all" script.
Thank you for your answer.
Ben|||You can run SSMS in "SQLCMD mode" (the Query menu) or use SQLCMD.EXE. If you do this, you can use :r
to have the tool to read a file. The file cannot contain GO (batch separator), at lest this was a
restriction last time I tested (2000).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
> On May 21, 4:47 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
>> What tool are you using to run the script? Also, does the script file exist on the server machine
>> or
>> the client machine?
>> --
>> Tibor Karaszi, SQL Server
>> MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/tibor_karaszi
>> <benjamin.am...@.gmail.com> wrote in message
>> news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
>> > Hi,
>> > Is it possible to open a sql file a run the content from another file?
>> > MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
>> > refman/5.0/en/batch-commands.html).
>> > I saw this news
>> >http://groups.google.com.au/group/microsoft.public.sqlserver.server/b...
>> > but it is not exactly what I want to do.
>> > Let me know if it's possible.
>> > Thank you.
>> > Ben
> I'm using the SQL Server Management Studio.
> The file will be run on the client machine.
> The reason why I'm asking for this function is because we have 300+
> SQL script files that need to be ran in a specific order. Opening them
> 1 by 1 and running them using the manager is *really* time consuming.
> The final script that will run all the others will be used by my
> client as a unique "run all" script.
> Thank you for your answer.
> Ben
>|||Since you need to run the 300+ files in a certain order, I would use a
simple vbscript and SMO to do the executes. Use the FileSystemObject to
open each file in the correct order (dos dir /b command output to file will
get you the list of files, which you can then order as appropriate), and
execute against a smo connection to the appropriate server/db.
--
TheSQLGuru
President
Indicium Resources, Inc.
<benjamin.amiot@.gmail.com> wrote in message
news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
> On May 21, 4:47 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
>> What tool are you using to run the script? Also, does the script file
>> exist on the server machine or
>> the client machine?
>> --
>> Tibor Karaszi, SQL Server
>> MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/tibor_karaszi
>> <benjamin.am...@.gmail.com> wrote in message
>> news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
>> > Hi,
>> > Is it possible to open a sql file a run the content from another file?
>> > MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
>> > refman/5.0/en/batch-commands.html).
>> > I saw this news
>> >http://groups.google.com.au/group/microsoft.public.sqlserver.server/b...
>> > but it is not exactly what I want to do.
>> > Let me know if it's possible.
>> > Thank you.
>> > Ben
> I'm using the SQL Server Management Studio.
> The file will be run on the client machine.
> The reason why I'm asking for this function is because we have 300+
> SQL script files that need to be ran in a specific order. Opening them
> 1 by 1 and running them using the manager is *really* time consuming.
> The final script that will run all the others will be used by my
> client as a unique "run all" script.
> Thank you for your answer.
> Ben
>|||On May 22, 4:30 am, "TheSQLGuru" <kgbo...@.earthlink.net> wrote:
> Since you need to run the 300+ files in a certain order, I would use a
> simple vbscript and SMO to do the executes. Use the FileSystemObject to
> open each file in the correct order (dos dir /b command output to file will
> get you the list of files, which you can then order as appropriate), and
> execute against a smo connection to the appropriate server/db.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> <benjamin.am...@.gmail.com> wrote in message
> news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
> > On May 21, 4:47 pm, "Tibor Karaszi"
> > <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> >> What tool are you using to run the script? Also, does the script file
> >> exist on the server machine or
> >> the client machine?
> >> --
> >> Tibor Karaszi, SQL Server
> >> MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/...
> >> <benjamin.am...@.gmail.com> wrote in message
> >>news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
> >> > Hi,
> >> > Is it possible to open a sql file a run the content from another file?
> >> > MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
> >> > refman/5.0/en/batch-commands.html).
> >> > I saw this news
> >> >http://groups.google.com.au/group/microsoft.public.sqlserver.server/b...
> >> > but it is not exactly what I want to do.
> >> > Let me know if it's possible.
> >> > Thank you.
> >> > Ben
> > I'm using the SQL Server Management Studio.
> > The file will be run on the client machine.
> > The reason why I'm asking for this function is because we have 300+
> > SQL script files that need to be ran in a specific order. Opening them
> > 1 by 1 and running them using the manager is *really* time consuming.
> > The final script that will run all the others will be used by my
> > client as a unique "run all" script.
> > Thank you for your answer.
> > Ben
Ok,
I'll try both method and see which one is the easiest to implement.
Thank you for your messages.
Ben|||On May 21, 6:43 pm, "Tibor Karaszi"
<tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> You can run SSMS in "SQLCMD mode" (the Query menu) or use SQLCMD.EXE. If you do this, you can use :r
> to have the tool to read a file. The file cannot contain GO (batch separator), at lest this was a
> restriction last time I tested (2000).
> --
> Tibor Karaszi, SQL Server MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/tibor_karaszi
> <benjamin.am...@.gmail.com> wrote in message
> news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
> > On May 21, 4:47 pm, "Tibor Karaszi"
> > <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> >> What tool are you using to run the script? Also, does the script file exist on the server machine
> >> or
> >> the client machine?
> >> --
> >> Tibor Karaszi, SQL Server
> >> MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/...
> >> <benjamin.am...@.gmail.com> wrote in message
> >>news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
> >> > Hi,
> >> > Is it possible to open a sql file a run the content from another file?
> >> > MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
> >> > refman/5.0/en/batch-commands.html).
> >> > I saw this news
> >> >http://groups.google.com.au/group/microsoft.public.sqlserver.server/b...
> >> > but it is not exactly what I want to do.
> >> > Let me know if it's possible.
> >> > Thank you.
> >> > Ben
> > I'm using the SQL Server Management Studio.
> > The file will be run on the client machine.
> > The reason why I'm asking for this function is because we have 300+
> > SQL script files that need to be ran in a specific order. Opening them
> > 1 by 1 and running them using the manager is *really* time consuming.
> > The final script that will run all the others will be used by my
> > client as a unique "run all" script.
> > Thank you for your answer.
> > Ben
Hi Tibor,
Thanks for your solution, but I won't be able to us it as most of the
files are using GO in them. I can't remove this command as the scripts
are generated by different teams in the company.
I'll try the solution of TheSQLGuru.
Ben|||> Thanks for your solution, but I won't be able to us it as most of the
> files are using GO in them. I can't remove this command as the scripts
> are generated by different teams in the company.
This is one of the reasons to always read BOL. Reading current BOL, it seems like you *can* have
batch terminator in the inner script. Quotes from BOL:
"If the file contains Transact-SQL statements that arenot followed by GO, you must enter GO on the
line that follows :r."
"The file will be read and executed after a batch terminator is encountered. You can issue multiple
:r commands. The file may include any sqlcmd command. This includes the batch terminator GO."
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179790434.126680.210820@.r3g2000prh.googlegroups.com...
> On May 21, 6:43 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
>> You can run SSMS in "SQLCMD mode" (the Query menu) or use SQLCMD.EXE. If you do this, you can use
>> :r
>> to have the tool to read a file. The file cannot contain GO (batch separator), at lest this was a
>> restriction last time I tested (2000).
>> --
>> Tibor Karaszi, SQL Server
>> MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/tibor_karaszi
>> <benjamin.am...@.gmail.com> wrote in message
>> news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
>> > On May 21, 4:47 pm, "Tibor Karaszi"
>> > <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
>> >> What tool are you using to run the script? Also, does the script file exist on the server
>> >> machine
>> >> or
>> >> the client machine?
>> >> --
>> >> Tibor Karaszi, SQL Server
>> >> MVPhttp://www.karaszi.com/sqlserver/default.asphttp://sqlblog.com/blogs/...
>> >> <benjamin.am...@.gmail.com> wrote in message
>> >>news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
>> >> > Hi,
>> >> > Is it possible to open a sql file a run the content from another file?
>> >> > MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
>> >> > refman/5.0/en/batch-commands.html).
>> >> > I saw this news
>> >> >http://groups.google.com.au/group/microsoft.public.sqlserver.server/b...
>> >> > but it is not exactly what I want to do.
>> >> > Let me know if it's possible.
>> >> > Thank you.
>> >> > Ben
>> > I'm using the SQL Server Management Studio.
>> > The file will be run on the client machine.
>> > The reason why I'm asking for this function is because we have 300+
>> > SQL script files that need to be ran in a specific order. Opening them
>> > 1 by 1 and running them using the manager is *really* time consuming.
>> > The final script that will run all the others will be used by my
>> > client as a unique "run all" script.
>> > Thank you for your answer.
>> > Ben
> Hi Tibor,
> Thanks for your solution, but I won't be able to us it as most of the
> files are using GO in them. I can't remove this command as the scripts
> are generated by different teams in the company.
> I'll try the solution of TheSQLGuru.
> Ben
>

Open a script file for a script

Hi,
Is it possible to open a sql file a run the content from another file?
MySQL can do it using the "source" function. (http://dev.mysql.com/doc/
refman/5.0/en/batch-commands.html).
I saw this news
0f09197e729ee" target="_blank">http://groups.google.com.au/group/m...
0f09197e729ee
but it is not exactly what I want to do.
Let me know if it's possible.
Thank you.
BenWhat tool are you using to run the script? Also, does the script file exist
on the server machine or
the client machine?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
> Hi,
> Is it possible to open a sql file a run the content from another file?
> mysql can do it using the "source" function. (http://dev.mysql.com/doc/
> refman/5.0/en/batch-commands.html).
> I saw this news
> ea0f09197e729ee" target="_blank">http://groups.google.com.au/group/m...ea0f09197e729ee
> but it is not exactly what I want to do.
> Let me know if it's possible.
> Thank you.
> Ben
>|||On May 21, 4:47 pm, "Tibor Karaszi"
<tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:[vbcol=seagreen]
> What tool are you using to run the script? Also, does the script file exis
t on the server machine or
> the client machine?
> --
> Tibor Karaszi, SQL Server MVPhttp://www.karaszi.com/sqlserver/default.asph
ttp://sqlblog.com/blogs/tibor_karaszi
> <benjamin.am...@.gmail.com> wrote in message
> news:1179729481.030659.246610@.n15g2000prd.googlegroups.com...
>
>
>
>
>
>
>
I'm using the SQL Server Management Studio.
The file will be run on the client machine.
The reason why I'm asking for this function is because we have 300+
SQL script files that need to be ran in a specific order. Opening them
1 by 1 and running them using the manager is *really* time consuming.
The final script that will run all the others will be used by my
client as a unique "run all" script.
Thank you for your answer.
Ben|||You can run SSMS in "SQLCMD mode" (the Query menu) or use SQLCMD.EXE. If you
do this, you can use :r
to have the tool to read a file. The file cannot contain GO (batch separator
), at lest this was a
restriction last time I tested (2000).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
> On May 21, 4:47 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> I'm using the SQL Server Management Studio.
> The file will be run on the client machine.
> The reason why I'm asking for this function is because we have 300+
> SQL script files that need to be ran in a specific order. Opening them
> 1 by 1 and running them using the manager is *really* time consuming.
> The final script that will run all the others will be used by my
> client as a unique "run all" script.
> Thank you for your answer.
> Ben
>|||Since you need to run the 300+ files in a certain order, I would use a
simple vbscript and SMO to do the executes. Use the FileSystemObject to
open each file in the correct order (dos dir /b command output to file will
get you the list of files, which you can then order as appropriate), and
execute against a smo connection to the appropriate server/db.
TheSQLGuru
President
Indicium Resources, Inc.
<benjamin.amiot@.gmail.com> wrote in message
news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
> On May 21, 4:47 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> I'm using the SQL Server Management Studio.
> The file will be run on the client machine.
> The reason why I'm asking for this function is because we have 300+
> SQL script files that need to be ran in a specific order. Opening them
> 1 by 1 and running them using the manager is *really* time consuming.
> The final script that will run all the others will be used by my
> client as a unique "run all" script.
> Thank you for your answer.
> Ben
>|||On May 22, 4:30 am, "TheSQLGuru" <kgbo...@.earthlink.net> wrote:[vbcol=seagreen]
> Since you need to run the 300+ files in a certain order, I would use a
> simple vbscript and SMO to do the executes. Use the FileSystemObject to
> open each file in the correct order (dos dir /b command output to file wil
l
> get you the list of files, which you can then order as appropriate), and
> execute against a smo connection to the appropriate server/db.
> --
> TheSQLGuru
> President
> Indicium Resources, Inc.
> <benjamin.am...@.gmail.com> wrote in message
> news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
Ok,
I'll try both method and see which one is the easiest to implement.
Thank you for your messages.
Ben|||On May 21, 6:43 pm, "Tibor Karaszi"
<tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:[vbcol=seagreen]
> You can run SSMS in "SQLCMD mode" (the Query menu) or use SQLCMD.EXE. If y
ou do this, you can use :r
> to have the tool to read a file. The file cannot contain GO (batch separat
or), at lest this was a
> restriction last time I tested (2000).
> --
> Tibor Karaszi, SQL Server MVPhttp://www.karaszi.com/sqlserver/default.asph
ttp://sqlblog.com/blogs/tibor_karaszi
> <benjamin.am...@.gmail.com> wrote in message
> news:1179731210.026500.253250@.r3g2000prh.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
Hi Tibor,
Thanks for your solution, but I won't be able to us it as most of the
files are using GO in them. I can't remove this command as the scripts
are generated by different teams in the company.
I'll try the solution of TheSQLGuru.
Ben|||> Thanks for your solution, but I won't be able to us it as most of the
> files are using GO in them. I can't remove this command as the scripts
> are generated by different teams in the company.
This is one of the reasons to always read BOL. Reading current BOL, it seems
like you *can* have
batch terminator in the inner script. Quotes from BOL:
"If the file contains Transact-SQL statements that arenot followed by GO, yo
u must enter GO on the
line that follows :r."
"The file will be read and executed after a batch terminator is encountered.
You can issue multiple
:r commands. The file may include any sqlcmd command. This includes the batc
h terminator GO."
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
<benjamin.amiot@.gmail.com> wrote in message
news:1179790434.126680.210820@.r3g2000prh.googlegroups.com...
> On May 21, 6:43 pm, "Tibor Karaszi"
> <tibor_please.no.email_kara...@.hotmail.nomail.com> wrote:
> Hi Tibor,
> Thanks for your solution, but I won't be able to us it as most of the
> files are using GO in them. I can't remove this command as the scripts
> are generated by different teams in the company.
> I'll try the solution of TheSQLGuru.
> Ben
>

Saturday, February 25, 2012

Only functions and extended stored procedures can be executed from within a function

Hi everybody,

When i try to excute a function i got the following error message:
" Only functions and extended stored procedures can be executed from within a function."

I wrote the following stored procedure wich update the sequence id :
---------------------
CREATE PROCEDURE dbo.pr_get_id_sequence
(@.p_nom_sequence varchar(100),
@.p_nom_table varchar(100),
@.p_id_sequence numeric OUTPUT ) AS

-- return an error if sequence does not exist
-- so we will know if someone truncates the table
DECLARE @.v_id_sequence numeric

SET @.v_id_sequence = -1

UPDATE GST_SEQUENCE
SET @.v_id_sequence = ID_SEQUENCE = ID_SEQUENCE + 1
WHERE NOM_SEQUENCE = @.p_nom_sequence
AND NOM_TABLE = @.p_nom_table

SET @.p_id_sequence = @.v_id_sequence
RETURN @.p_id_sequence

Then, i wrote the following function which return the last value of sequence id:
--------------------
CREATE FUNCTION dbo.fu_get_id_sequence
(@.p_nom_sequence varchar(100),
@.p_nom_table varchar(100)
) returns int AS

BEGIN
DECLARE @.v_id_sequence numeric

Execute dbo.pr_get_id_sequence @.p_nom_sequence, @.p_nom_table, @.v_id_sequence

RETURN @.v_id_sequence

END

And, when i try to call the function, i got the error message:
-------------------
select dbo.fu_get_id_sequence ('SEQ', 'TABLE')

By the way i'm using SQL Server 2005. Also, I tried to create the function by incliding the UPDATE statement but it didn't work.

Can anyone help me ?
Thank you

PaulThe error messages clearly point to the source of error: you cannot call stored procedures, as well as perform updates, deletes and inserts in T-SQL functions. That's why you cannot execute your function. You'll have to create stored procedure instead of function to be able to call another stored procedure. Also, all output parameters should be explicitly marked as output when you call a stored procedure:

Execute dbo.pr_get_id_sequence @.p_nom_sequence, @.p_nom_table, @.v_id_sequence output