Microsoft Data Access Blocks (SqlHelper) - Executing 2 SQL commands

F

FyinsFlip

Microsoft Data Access Blocks (SqlHelper) you can do an insert and get
the new row ID without using a stored procedure?

DataMembers.Car_IDObject = SqlHelper.ExecuteScalar( _connectionString ,
"INSERT INTO tCar (name) VALUES ('Ford') SELECT CAST(@@Identity AS
INTEGER) ", FillSqlParameter());

Is there a new line symbol I can add before the SELECT, like a ';'
or something?

--
Sincerely,
David Dimmer

Quality Builders of ASP.NET Web Sites

B: 414.769.1233
M: 414.688.3941
W: http://www.milwaukeewebdesigners.com/
 
G

Guest

You can separate commands with a ";", if you want to string commands.
Personally, I would go to stored procedures, but you can string any number of
commands. WHen you complete the batch, add a "; GO" to the end to make sure
the entire batch runs.

Also, use "SCOPE_IDENTITY" instead of "@@IDENTITY", as high traffic sites
could yield a wrong answer.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
B

bruce barker

your syntax is correct, no linebreaks are required to seperate statements, a
space is fine. ExecuteScalar returns the first column of the first row of
the first result set. the way you have the sql coded, you get two result
sets, but thats easily fixed. try:

DataMembers.Car_IDObject = SqlHelper.ExecuteScalar( _connectionString ,
"set nocount on INSERT INTO tCar (name) VALUES ('Ford') SELECT
CAST(scope_identity() AS
INTEGER) ",
FillSqlParameter());


-- bruce (sqlwork.com)


| Microsoft Data Access Blocks (SqlHelper) you can do an insert and get
| the new row ID without using a stored procedure?
|
| DataMembers.Car_IDObject = SqlHelper.ExecuteScalar( _connectionString ,
| "INSERT INTO tCar (name) VALUES ('Ford') SELECT CAST(@@Identity AS
| INTEGER) ", FillSqlParameter());
|
| Is there a new line symbol I can add before the SELECT, like a ';'
| or something?
|
| --
| Sincerely,
| David Dimmer
|
| Quality Builders of ASP.NET Web Sites
|
| B: 414.769.1233
| M: 414.688.3941
| W: http://www.milwaukeewebdesigners.com/
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top