Showing posts with label appreciate. Show all posts
Showing posts with label appreciate. Show all posts

Wednesday, March 7, 2012

How to retrieve last inserted ID(Auto-Number) then perform another Insert Statement?

Hi All,

I hope you could help me in retrieving the last inserted ID(Auto-Number) then perform another Insert Statement?

I would really much appreciate it. I am coding in VB and am uisng

Visual Web Developer 2005 Express Edition and Microsoft SQL Sever

Management Studio Express.

Thanks alot.

-- Sam

My Codes:

Dim SQLStr5 As String = "INSERT INTO

NotesDetails(Notes_Level,Notes_Subject,Notes_Type,Notes_Year,Notes_Desc)

VALUES ('" & ddl_level.SelectedValue & "','" &

ddl_sub.SelectedValue & "','" & rbl_type.SelectedValue &

"','" & ddl_year.SelectedValue & "','" & tb_desc.Text &

"')"

Dim con5 As New SqlConnection(connstring)

con5.Open()

Dim cmd5 As New SqlCommand(SQLStr5, con5)

cmd5.ExecuteNonQuery()

con5.Close()

'' Need to get last inserted ID to Insert into the next Statement. (Notes_ID)

Dim SQLStr5a As String = "INSERT INTO

NotesComments(Notes_ID,Notes_Comments) VALUES ('" &

ddl_level.SelectedValue & "','" & tb_comments.Text & "')"

Dim con5a As New SqlConnection(connstring)

con5a.Open()

Dim cmd5a As New SqlCommand(SQLStr5a, con5a)

cmd5a.ExecuteNonQuery()

con5a.Close()

The last identity can be fetched using the SCOPE_IDENTITY() function, be aware that you have to do that within the same scope. As an additional new feature of SQL Server 2005 you could use the OUTPUT clause which give you the availbility to return values within the same DML statement. See more informations and samples in the BOL.

HTH, Jens Suessmeyer.


http://www.sqlserver2005.de

Sunday, February 19, 2012

How to restore Transaction log

Hello everyone,
I have a problem which is driving me crazy. I can't figure it out how I'm
going to solve this problem but I appreciate any help in this regards.
I'm configuring a log shipping server and it's almost done. The only problem
I'm having is restoring logs from the primary server. the primary server is
appending logs in a log file. If I use the RESTORE HEADERONLY command to
read the file I can see lot's of backup sets like FILE 1, FILE 2, FILE
3....Now I know how to use the restore command but my problem is using TSQL
how can I restore this file in one go...I mean I don't have to restore
backup sets one by one...
Thanx.
Muneeb.You cannot make a single RESTORE command do several RESTORE operations. You
need to do several RESTORE executions.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Muneeb Ahmed" <muneeb.ahmed@.mhs.com> wrote in message
news:eBKYz4UDEHA.2564@.TK2MSFTNGP11.phx.gbl...
> Hello everyone,
> I have a problem which is driving me crazy. I can't figure it out how I'm
> going to solve this problem but I appreciate any help in this regards.
> I'm configuring a log shipping server and it's almost done. The only
problem
> I'm having is restoring logs from the primary server. the primary server
is
> appending logs in a log file. If I use the RESTORE HEADERONLY command to
> read the file I can see lot's of backup sets like FILE 1, FILE 2, FILE
> 3....Now I know how to use the restore command but my problem is using
TSQL
> how can I restore this file in one go...I mean I don't have to restore
> backup sets one by one...
> Thanx.
> Muneeb.
>