Showing posts with label declare. Show all posts
Showing posts with label declare. Show all posts

Thursday, March 22, 2012

Do for each - how to write it more graceful?

"Do something for each row of the query"
The only way I know is:

--
declare @.C cursor
set @.C= cursor for
select F from T where ...
declare @.F int
open @.C
while 0=0 begin
fetch next from @.C into @.F
if not(@.@.FETCH_STATUS = 0) break
exec myStoredProc @.F
end
close @.C
deallocate @.C
--

How to write it simpler, maybe with implicit cursors?
For example, in the Borland Interbase it would be like:

--
declare variable F integer;
for select F from T into :F
do execute procedure myStoredProc :F;
--

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!What does myStoredProc actually do? If the procedure just does some data
manipulation then maybe you can rewrite that code based on your cursor
query. For example:

SELECT ...
FROM Something
WHERE f
IN
(select F from T where ...)

As another alternative to a cursor you can try something like this, which
may be reasonably acceptable if the column F is unique and indexed.

DECLARE @.f INTEGER

WHILE EXISTS
(SELECT *
FROM T
WHERE f>@.f
OR @.f IS NULL)
BEGIN
SET @.f =
(SELECT MIN(f)
FROM T
WHERE f>@.f
OR @.f IS NULL)
EXEC myStoredProc @.F
END

--
David Portas
SQL Server MVP
--|||Thank you for these two ideas!
But... first case is not quite fit for me. My stored procedure is too
complicated, it makes some queries to remote server, calls some extended
procedures and so on.
Second case seems better, but (IMHO) it's a trick. And this trick (IMHO)
is too slow relative to case with cursor and fetch. And it looks like
T-SQL syntax limitation - no any common and short way to do something
for each row, like cycle by select :(

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||Other options:

1) Build a Dynamic SQL statement in a loop and then execute it.

2) Put the loop in the middle tier or client code.

3) For non-production use you could try the undocumented xp_execresultset.
Example:

EXEC master..xp_execresultset
'SELECT ''EXEC myStoredProc ''+CAST(f AS VARCHAR) FROM T','DBNAME'

--
David Portas
SQL Server MVP
--|||Evgeny Gopengauz (evgop@.ucs.ru) writes:
> "Do something for each row of the query"
> The only way I know is:
> --
> declare @.C cursor
> set @.C= cursor for
> select F from T where ...
> declare @.F int
> open @.C
> while 0=0 begin
> fetch next from @.C into @.F
> if not(@.@.FETCH_STATUS = 0) break
> exec myStoredProc @.F
> end
> close @.C
> deallocate @.C
> --
> How to write it simpler, maybe with implicit cursors?

There are a couple of options, you can use SELECT MIN or SELECT TOP 1.
I would however recommend to stick with the cursors, they are in my
opinion the best way to iterate when you need to iterate. Solutions
with MIN or TOP 1 can have bad performance if there is no good index.

One tip is to make the cursor INSENSITIVE, since keyset-driven cursors
(the default) can sometimes have absymal performance when nailing
down which rows to operate on. Also insensitive saves you from
surprises if you update rows selected by the cursor.

Finally, I never use cursor variables, but always static names.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.aspsql

Sunday, March 11, 2012

division in SQL results in 0

division in SQL results in 0 Posted on: 01/09/2006 09:20:13



declare @.x int, @.y int, @.f float
set @.x = 100
set @.y = 2000
set @.f = @.x / @.y
print @.x
print @.y
print @.f

in the above code @.f prints as 0 instead of 0.05
how would i correct that?

Try this:

declare @.x int, @.y int, @.f float
set @.x = 100
set @.y = 2000
set @.f = @.x / (@.y * 1.0)
print @.x
print @.y
print @.f

Friday, February 17, 2012

distributed transaction MSQL<->oracle

I'm trying to execute this:
create table #tt (nro_parte_erp varchar(50))
declare @.query as varchar(4000)
declare @.estado as char
set @.estado=3D'1'
set @.query =3D'
select
nro_parte_erp=3D nro_parte
from openquery(desarrollo_baan,''
select
nro_parte,
from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
a=2Eorden_fab_1=3Db.orden_fab and estado_orden=3D'+@.estado+''') a
order by orden_fab_1,operacion'
insert into #tt
exec (@.query)
where desarrollo_baan is a linked server to oracle
but i receive this error:
No se puede realizar la operaci=F3n. El proveedor OLE DB 'MSDAORA' no
pudo iniciar una transacci=F3n distribuida.
Traza de error de OLE DB [OLE/DB Provider 'MSDAORA'
ITransactionJoin::JoinTransaction returned 0x8000d048].
in english something like
The operation could not be performed because the OLE DB provider
'OraOLEDB.Oracle' was unable to begin a distributed transaction.
Error Trace of OLE DB [OLE/DB Provider 'MSDAORA'
ITransactionJoin::JoinTransaction returned 0x8000d048].
And i'm sure that MSDTC is running and i tried re-creating my server.
Some body can help me?
Can you run a simple select statement via link server?
"jreategui@.gmail.com" wrote:

> I'm trying to execute this:
> create table #tt (nro_parte_erp varchar(50))
> declare @.query as varchar(4000)
> declare @.estado as char
> set @.estado='1'
> set @.query ='
> select
> nro_parte_erp= nro_parte
> from openquery(desarrollo_baan,''
> select
> nro_parte,
> from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
> a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
> order by orden_fab_1,operacion'
> insert into #tt
> exec (@.query)
>
> where desarrollo_baan is a linked server to oracle
> but i receive this error:
> No se puede realizar la operación. El proveedor OLE DB 'MSDAORA' no
> pudo iniciar una transacción distribuida.
> Traza de error de OLE DB [OLE/DB Provider 'MSDAORA'
> ITransactionJoin::JoinTransaction returned 0x8000d048].
> in english something like
> The operation could not be performed because the OLE DB provider
> 'OraOLEDB.Oracle' was unable to begin a distributed transaction.
> Error Trace of OLE DB [OLE/DB Provider 'MSDAORA'
> ITransactionJoin::JoinTransaction returned 0x8000d048].
>
> And i'm sure that MSDTC is running and i tried re-creating my server.
>
> Some body can help me?
>
|||Yes, i tested the same code without the insert statment and it's run.
I mean, this code run perfect:
create table #tt (nro_parte_erp varchar(50))
declare @.query as varchar(4000)
declare @.estado as char
set @.estado='1'
set @.query ='
select
nro_parte_erp= nro_parte
from openquery(desarrollo_baan,''
select
nro_parte,
from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
order by orden_fab_1,operacion'
/*insert into #tt */
exec (@.query)
|||Looks like your problem is not with the link Server, b/c it is letting you
execute the command. Is the output less than varchar(50)? I have noticed that
sometimes when you use different software the output from exec(@.SQL) will not
get into the temp table. Try insert into a temp table and see if that works?
"jreategui@.gmail.com" wrote:
insert statment and it's run.
> I mean, this code run perfect:
> Yes, i tested the same code without the
> create table #tt (nro_parte_erp varchar(50))
> declare @.query as varchar(4000)
> declare @.estado as char
> set @.estado='1'
> set @.query ='
> select
> nro_parte_erp= nro_parte
>
> from openquery(desarrollo_baan,''
> select
> nro_parte,
> from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
> a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
>
> order by orden_fab_1,operacion'
>
> /*insert into #tt */
> exec (@.query)
>
|||That's correct, the problem is not the linked server, the query run
perfect. The really problem is making the insert in the temp table with
the data that return the query to the linked server.
I tried making some manuals insert to the temp table and that work, but
with the linked server don't

distributed transaction MSQL<->oracle

I'm trying to execute this:
create table #tt (nro_parte_erp varchar(50))
declare @.query as varchar(4000)
declare @.estado as char
set @.estado=3D'1'
set @.query =3D'
select
nro_parte_erp=3D nro_parte
from openquery(desarrollo_baan,''
select
nro_parte,
from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
a=2Eorden_fab_1=3Db.orden_fab and estado_orden=3D'+@.estado+''') a
order by orden_fab_1,operacion'
insert into #tt
exec (@.query)
where desarrollo_baan is a linked server to oracle
but i receive this error:
No se puede realizar la operaci=F3n. El proveedor OLE DB 'MSDAORA' no
pudo iniciar una transacci=F3n distribuida.
Traza de error de OLE DB [OLE/DB Provider 'MSDAORA'
ITransactionJoin::JoinTransaction returned 0x8000d048].
in english something like
The operation could not be performed because the OLE DB provider
'OraOLEDB.Oracle' was unable to begin a distributed transaction.
Error Trace of OLE DB [OLE/DB Provider 'MSDAORA'
ITransactionJoin::JoinTransaction returned 0x8000d048].
And i'm sure that MSDTC is running and i tried re-creating my server.
Some body can help me'Can you run a simple select statement via link server?
"jreategui@.gmail.com" wrote:

> I'm trying to execute this:
> create table #tt (nro_parte_erp varchar(50))
> declare @.query as varchar(4000)
> declare @.estado as char
> set @.estado='1'
> set @.query ='
> select
> nro_parte_erp= nro_parte
> from openquery(desarrollo_baan,''
> select
> nro_parte,
> from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
> a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
> order by orden_fab_1,operacion'
> insert into #tt
> exec (@.query)
>
> where desarrollo_baan is a linked server to oracle
> but i receive this error:
> No se puede realizar la operación. El proveedor OLE DB 'MSDAORA' no
> pudo iniciar una transacción distribuida.
> Traza de error de OLE DB [OLE/DB Provider 'MSDAORA'
> ITransactionJoin::JoinTransaction returned 0x8000d048].
> in english something like
> The operation could not be performed because the OLE DB provider
> 'OraOLEDB.Oracle' was unable to begin a distributed transaction.
> Error Trace of OLE DB [OLE/DB Provider 'MSDAORA'
> ITransactionJoin::JoinTransaction returned 0x8000d048].
>
> And i'm sure that MSDTC is running and i tried re-creating my server.
>
> Some body can help me'
>|||Yes, i tested the same code without the insert statment and it's run.
I mean, this code run perfect:
create table #tt (nro_parte_erp varchar(50))
declare @.query as varchar(4000)
declare @.estado as char
set @.estado='1'
set @.query ='
select
nro_parte_erp= nro_parte
from openquery(desarrollo_baan,''
select
nro_parte,
from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
order by orden_fab_1,operacion'
/*insert into #tt */
exec (@.query)|||Looks like your problem is not with the link Server, b/c it is letting you
execute the command. Is the output less than varchar(50)? I have noticed tha
t
sometimes when you use different software the output from exec(@.SQL) will no
t
get into the temp table. Try insert into a temp table and see if that works?
"jreategui@.gmail.com" wrote:
insert statment and it's run.
> I mean, this code run perfect:
> Yes, i tested the same code without the
> create table #tt (nro_parte_erp varchar(50))
> declare @.query as varchar(4000)
> declare @.estado as char
> set @.estado='1'
> set @.query ='
> select
> nro_parte_erp= nro_parte
>
> from openquery(desarrollo_baan,''
> select
> nro_parte,
> from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
> a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
>
> order by orden_fab_1,operacion'
>
> /*insert into #tt */
> exec (@.query)
>|||That's correct, the problem is not the linked server, the query run
perfect. The really problem is making the insert in the temp table with
the data that return the query to the linked server.
I tried making some manuals insert to the temp table and that work, but
with the linked server don't

distributed transaction MSQL<->oracle

I'm trying to execute this:
create table #tt (nro_parte_erp varchar(50))
declare @.query as varchar(4000)
declare @.estado as char
set @.estado=3D'1'
set @.query =3D'
select
nro_parte_erp=3D nro_parte
from openquery(desarrollo_baan,''
select
nro_parte,
from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
a=2Eorden_fab_1=3Db.orden_fab and estado_orden=3D'+@.estado+''') a
order by orden_fab_1,operacion'
insert into #tt
exec (@.query)
where desarrollo_baan is a linked server to oracle
but i receive this error:
No se puede realizar la operaci=F3n. El proveedor OLE DB 'MSDAORA' no
pudo iniciar una transacci=F3n distribuida.
Traza de error de OLE DB [OLE/DB Provider 'MSDAORA'
ITransactionJoin::JoinTransaction returned 0x8000d048].
in english something like
The operation could not be performed because the OLE DB provider
'OraOLEDB.Oracle' was unable to begin a distributed transaction.
Error Trace of OLE DB [OLE/DB Provider 'MSDAORA'
ITransactionJoin::JoinTransaction returned 0x8000d048].
And i'm sure that MSDTC is running and i tried re-creating my server.
Some body can help me'Can you run a simple select statement via link server?
"jreategui@.gmail.com" wrote:
> I'm trying to execute this:
> create table #tt (nro_parte_erp varchar(50))
> declare @.query as varchar(4000)
> declare @.estado as char
> set @.estado='1'
> set @.query ='
> select
> nro_parte_erp= nro_parte
> from openquery(desarrollo_baan,''
> select
> nro_parte,
> from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
> a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
> order by orden_fab_1,operacion'
> insert into #tt
> exec (@.query)
>
> where desarrollo_baan is a linked server to oracle
> but i receive this error:
> No se puede realizar la operación. El proveedor OLE DB 'MSDAORA' no
> pudo iniciar una transacción distribuida.
> Traza de error de OLE DB [OLE/DB Provider 'MSDAORA'
> ITransactionJoin::JoinTransaction returned 0x8000d048].
> in english something like
> The operation could not be performed because the OLE DB provider
> 'OraOLEDB.Oracle' was unable to begin a distributed transaction.
> Error Trace of OLE DB [OLE/DB Provider 'MSDAORA'
> ITransactionJoin::JoinTransaction returned 0x8000d048].
>
> And i'm sure that MSDTC is running and i tried re-creating my server.
>
> Some body can help me'
>|||Yes, i tested the same code without the insert statment and it's run.
I mean, this code run perfect:
create table #tt (nro_parte_erp varchar(50))
declare @.query as varchar(4000)
declare @.estado as char
set @.estado='1'
set @.query ='
select
nro_parte_erp= nro_parte
from openquery(desarrollo_baan,''
select
nro_parte,
from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
order by orden_fab_1,operacion'
/*insert into #tt */
exec (@.query)|||Looks like your problem is not with the link Server, b/c it is letting you
execute the command. Is the output less than varchar(50)? I have noticed that
sometimes when you use different software the output from exec(@.SQL) will not
get into the temp table. Try insert into a temp table and see if that works?
"jreategui@.gmail.com" wrote:
insert statment and it's run.
> I mean, this code run perfect:
> Yes, i tested the same code without the
> create table #tt (nro_parte_erp varchar(50))
> declare @.query as varchar(4000)
> declare @.estado as char
> set @.estado='1'
> set @.query ='
> select
> nro_parte_erp= nro_parte
>
> from openquery(desarrollo_baan,''
> select
> nro_parte,
> from BAAN.ORDENES_102 a,baan.set_of_cim_102 b where
> a.orden_fab_1=b.orden_fab and estado_orden='+@.estado+''') a
>
> order by orden_fab_1,operacion'
>
> /*insert into #tt */
> exec (@.query)
>|||That's correct, the problem is not the linked server, the query run
perfect. The really problem is making the insert in the temp table with
the data that return the query to the linked server.
I tried making some manuals insert to the temp table and that work, but
with the linked server don't