Home All Groups Group Topic Archive Search About

using 2 sql commands in 1 string?

Author
17 Mar 2006 12:36 AM
Lewis
I am using SQL server express 2005 and sending the string as an SQL text
query from C# 2005.
Perhaps I am missing something, but when I use

UPDATE    SomLast_num
SET              CUST_NUM = CUST_NUM + 1
WHERE     (CurrentRec = 1)

I get a respose of 1 as an integer. (I assume it means 1 record affected)

When I try to get the new record number by sending

UPDATE    SomLast_num SET CUST_NUM = CUST_NUM + 1 WHERE  CurrentRec = 1
SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1

I get an "unable to parse error"
Is there a way to send 2 SQL statements and use the last response as the
return value?
I tried separating them with ; and putting a go between them, but I still
get an unable to parse error.

--
"OK - I've inserted the second disk... now the're BOTH stuck  :(

Author
17 Mar 2006 12:59 AM
David Browne
Show quote
"Lewis" <Le***@discussions.microsoft.com> wrote in message
news:AFA62A62-CB8B-4E59-8549-FCED9348A277@microsoft.com...
>I am using SQL server express 2005 and sending the string as an SQL text
> query from C# 2005.
> Perhaps I am missing something, but when I use
>
> UPDATE    SomLast_num
> SET              CUST_NUM = CUST_NUM + 1
> WHERE     (CurrentRec = 1)
>
> I get a respose of 1 as an integer. (I assume it means 1 record affected)
>
> When I try to get the new record number by sending
>
> UPDATE    SomLast_num SET CUST_NUM = CUST_NUM + 1 WHERE  CurrentRec = 1
> SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
>
> I get an "unable to parse error"
> Is there a way to send 2 SQL statements and use the last response as the
> return value?
> I tried separating them with ; and putting a go between them, but I still
> get an unable to parse error.


Yes.  But there's a killer feature of SQL 2005 that makes this unnecessary:
the OUTPUT clause.  You can update the table and return the updated data in
a single statement.

UPDATE SomLast_num
SET CUST_NUM = CUST_NUM + 1
OUTPUT INSERTED.CUST_NUM
WHERE (CurrentRec = 1)


This will return a result set exactly like

SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1

..  This also helps insulate you from concurrency problems that you would
have with your approach.

David
Author
17 Mar 2006 1:18 AM
Lewis
That looks cool and the whole Idea was to reduce the concurrency issue.
But now it says I'm missing an INTO statement. Any idea where it goes?
--
"OK - I've inserted the second disk... now the're BOTH stuck  :(


Show quote
"David Browne" wrote:

>
> "Lewis" <Le***@discussions.microsoft.com> wrote in message
> news:AFA62A62-CB8B-4E59-8549-FCED9348A277@microsoft.com...
> >I am using SQL server express 2005 and sending the string as an SQL text
> > query from C# 2005.
> > Perhaps I am missing something, but when I use
> >
> > UPDATE    SomLast_num
> > SET              CUST_NUM = CUST_NUM + 1
> > WHERE     (CurrentRec = 1)
> >
> > I get a respose of 1 as an integer. (I assume it means 1 record affected)
> >
> > When I try to get the new record number by sending
> >
> > UPDATE    SomLast_num SET CUST_NUM = CUST_NUM + 1 WHERE  CurrentRec = 1
> > SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
> >
> > I get an "unable to parse error"
> > Is there a way to send 2 SQL statements and use the last response as the
> > return value?
> > I tried separating them with ; and putting a go between them, but I still
> > get an unable to parse error.
>
>
> Yes.  But there's a killer feature of SQL 2005 that makes this unnecessary:
> the OUTPUT clause.  You can update the table and return the updated data in
> a single statement.
>
> UPDATE SomLast_num
> SET CUST_NUM = CUST_NUM + 1
> OUTPUT INSERTED.CUST_NUM
> WHERE (CurrentRec = 1)
>
>
> This will return a result set exactly like
>
> SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
>
> ..  This also helps insulate you from concurrency problems that you would
> have with your approach.
>
> David
>
>
>
Author
17 Mar 2006 1:59 AM
Lewis
That did work.
It gives me that error about a missint INTO statement, but when I call the
command, it increments the number and returns the new number.

Thanks
--
"OK - I've inserted the second disk... now the're BOTH stuck  :(


Show quote
"David Browne" wrote:

>
> "Lewis" <Le***@discussions.microsoft.com> wrote in message
> news:AFA62A62-CB8B-4E59-8549-FCED9348A277@microsoft.com...
> >I am using SQL server express 2005 and sending the string as an SQL text
> > query from C# 2005.
> > Perhaps I am missing something, but when I use
> >
> > UPDATE    SomLast_num
> > SET              CUST_NUM = CUST_NUM + 1
> > WHERE     (CurrentRec = 1)
> >
> > I get a respose of 1 as an integer. (I assume it means 1 record affected)
> >
> > When I try to get the new record number by sending
> >
> > UPDATE    SomLast_num SET CUST_NUM = CUST_NUM + 1 WHERE  CurrentRec = 1
> > SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
> >
> > I get an "unable to parse error"
> > Is there a way to send 2 SQL statements and use the last response as the
> > return value?
> > I tried separating them with ; and putting a go between them, but I still
> > get an unable to parse error.
>
>
> Yes.  But there's a killer feature of SQL 2005 that makes this unnecessary:
> the OUTPUT clause.  You can update the table and return the updated data in
> a single statement.
>
> UPDATE SomLast_num
> SET CUST_NUM = CUST_NUM + 1
> OUTPUT INSERTED.CUST_NUM
> WHERE (CurrentRec = 1)
>
>
> This will return a result set exactly like
>
> SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
>
> ..  This also helps insulate you from concurrency problems that you would
> have with your approach.
>
> David
>
>
>
Author
17 Mar 2006 2:20 PM
Elton W
Hi Lewis,

You can run multi sql-commands in one string. But you should use ‘;’ to
separate sql commands:

sqlcommandObj.CommandText  = “sql_command_one; sql_command_two;…”;
sqlcommandObj. ExecuteNonQuery();

HTH

Elton Wang

Show quote
"Lewis" wrote:

> I am using SQL server express 2005 and sending the string as an SQL text
> query from C# 2005.
> Perhaps I am missing something, but when I use
>
> UPDATE    SomLast_num
> SET              CUST_NUM = CUST_NUM + 1
> WHERE     (CurrentRec = 1)
>
> I get a respose of 1 as an integer. (I assume it means 1 record affected)
>
> When I try to get the new record number by sending
>
> UPDATE    SomLast_num SET CUST_NUM = CUST_NUM + 1 WHERE  CurrentRec = 1
> SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
>
> I get an "unable to parse error"
> Is there a way to send 2 SQL statements and use the last response as the
> return value?
> I tried separating them with ; and putting a go between them, but I still
> get an unable to parse error.
>
> --
> "OK - I've inserted the second disk... now the're BOTH stuck  :(
Author
21 Mar 2006 6:57 PM
Lewis
Thanks Elton,
I had tried using ; but it gave me a parse error. Perhaps I had a second
error...
--
"OK - I've inserted the second disk... now the're BOTH stuck  :(


Show quote
"Elton W" wrote:

> Hi Lewis,
>
> You can run multi sql-commands in one string. But you should use ‘;’ to
> separate sql commands:
>
> sqlcommandObj.CommandText  = “sql_command_one; sql_command_two;…”;
> sqlcommandObj. ExecuteNonQuery();
>
> HTH
>
> Elton Wang
>
> "Lewis" wrote:
>
> > I am using SQL server express 2005 and sending the string as an SQL text
> > query from C# 2005.
> > Perhaps I am missing something, but when I use
> >
> > UPDATE    SomLast_num
> > SET              CUST_NUM = CUST_NUM + 1
> > WHERE     (CurrentRec = 1)
> >
> > I get a respose of 1 as an integer. (I assume it means 1 record affected)
> >
> > When I try to get the new record number by sending
> >
> > UPDATE    SomLast_num SET CUST_NUM = CUST_NUM + 1 WHERE  CurrentRec = 1
> > SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
> >
> > I get an "unable to parse error"
> > Is there a way to send 2 SQL statements and use the last response as the
> > return value?
> > I tried separating them with ; and putting a go between them, but I still
> > get an unable to parse error.
> >
> > --
> > "OK - I've inserted the second disk... now the're BOTH stuck  :(
Author
22 Mar 2006 1:56 AM
Elton W
It suppose work. Anyway, you can use Stored Procedure to achieve it.

HTH

Elton Wang

Show quote
"Lewis" wrote:

> Thanks Elton,
> I had tried using ; but it gave me a parse error. Perhaps I had a second
> error...
> --
> "OK - I've inserted the second disk... now the're BOTH stuck  :(
>
>
> "Elton W" wrote:
>
> > Hi Lewis,
> >
> > You can run multi sql-commands in one string. But you should use ‘;’ to
> > separate sql commands:
> >
> > sqlcommandObj.CommandText  = “sql_command_one; sql_command_two;…”;
> > sqlcommandObj. ExecuteNonQuery();
> >
> > HTH
> >
> > Elton Wang
> >
> > "Lewis" wrote:
> >
> > > I am using SQL server express 2005 and sending the string as an SQL text
> > > query from C# 2005.
> > > Perhaps I am missing something, but when I use
> > >
> > > UPDATE    SomLast_num
> > > SET              CUST_NUM = CUST_NUM + 1
> > > WHERE     (CurrentRec = 1)
> > >
> > > I get a respose of 1 as an integer. (I assume it means 1 record affected)
> > >
> > > When I try to get the new record number by sending
> > >
> > > UPDATE    SomLast_num SET CUST_NUM = CUST_NUM + 1 WHERE  CurrentRec = 1
> > > SELECT CUST_NUM FROM SomLast_num WHERE CurrentRec = 1
> > >
> > > I get an "unable to parse error"
> > > Is there a way to send 2 SQL statements and use the last response as the
> > > return value?
> > > I tried separating them with ; and putting a go between them, but I still
> > > get an unable to parse error.
> > >
> > > --
> > > "OK - I've inserted the second disk... now the're BOTH stuck  :(

AddThis Social Bookmark Button