Showing posts with label systems. Show all posts
Showing posts with label systems. Show all posts

Friday, March 23, 2012

How to get the last item added

Not 100% sure how to do this so I would appreciate some directions. I have 2 tables, Systems and Contacts. A system can have 1 to infinite contacts and contact can have 1 to infinite systems. So I use a 3rd table Sys_Con to add the contact needed for each system.
Now my question is once I add the System and contact how do I know for sure which ContactID to add in my Sys_Con table? Because while I am doing this operation maybe someone can add an other contact. So is there a way for my store proc Add_Contact to return the contactID needed for my store proc Add_Sys_Con ?

TABLE Sys_Con
SystemID int
ContactID int

Table Contact
ContactID int Identity
ContactName
...

Table System
SystemID
...After your insert

SELECT @.@.IDENTITY AS 'Identity'

Here is the resource:@.@.IDENTITY

hope this helps,
sivilian|||Thanks for your reply,
how do I get the identity back from my insert sp of my contact? I dont seem to be able to make this work. I get the error: "Procedure 'Add_Cl_Contact' expects parameter '@.ContactId', which was not supplied." on the line "iContactID = cmdSelect.Parameters("ContactId").Value"

Here is what I have up to now:


ALTER procedure dbo.Add_Cl_Contact
(
@.ContactNamenvarchar(75),
@.Departmentnvarchar (50)=null,
@.Titlenvarchar(50)=null,
@.Phone1char(20)=null,
@.Phone2char(20)=null,
@.Phone3char(20)=null,
@.Ext1char(10)=null,
@.Ext2char(10)=null,
@.Ext3char(10)=null,
@.Faxnvarchar(50)=null,
@.Emailnvarchar(50)=null,
@.ContactIdintOUTPUT
)
AS
insert into Cl_Contacts (
ContactName,
Department,
Title,
Phone1,
Phone2,
Phone3,
Ext1,
Ext2,
Ext3,
Fax,
Email
)
values (
@.ContactName,
@.Department,
@.Title,
@.Phone1,
@.Phone2,
@.Phone3,
@.Ext1,
@.Ext2,
@.Ext3,
@.Fax,
@.Email
)
Select @.ContactId = @.@.IDENTITY

'******************************************************************
'******************************************************************
Public Function Add_Cl_Contact(ByVal strContactName As String, _
ByVal strDep As String, ByVal strEmail As String, ByVal strExt1 As String, ByVal strExt2 As String, _
ByVal strExt3 As String, ByVal strFax As String, ByVal strPhone1 As String, ByVal strPhone2 As String, _
ByVal strPhone3 As String, ByVal strTitle As String, ByRef iContactID As Integer, ByRef strError As String) As Boolean
'******************************************************************
Dim bSuccess As Boolean = True
Dim connect As New SqlConnection(strConnection)
Dim cmdSelect As New SqlCommand("Add_Cl_Contact", connect)
Dim paramReturnValue As SqlParameter

cmdSelect.CommandType = CommandType.StoredProcedure
'PARAM
cmdSelect.Parameters.Add("@.ContactName", strContactName)
cmdSelect.Parameters.Add("@.Department", strDep)
cmdSelect.Parameters.Add("@.Title", strTitle)
cmdSelect.Parameters.Add("@.Phone1", strPhone1)
cmdSelect.Parameters.Add("@.Phone2", strPhone2)
cmdSelect.Parameters.Add("@.Phone3", strPhone3)
cmdSelect.Parameters.Add("@.Ext1", strExt1)
cmdSelect.Parameters.Add("@.Ext2", strExt2)
cmdSelect.Parameters.Add("@.Ext3", strExt3)
cmdSelect.Parameters.Add("@.Fax", strFax)
cmdSelect.Parameters.Add("@.Email", strEmail)

paramReturnValue = cmdSelect.Parameters.Add("ContactId", SqlDbType.Int)
paramReturnValue.Direction = ParameterDirection.ReturnValue

Try
connect.Open()
cmdSelect.ExecuteNonQuery()
iContactID = cmdSelect.Parameters("ContactId").Value
connect.Close()
Catch ex As Exception
bSuccess = False
strError = ex.Message
Finally
If connect.State = ConnectionState.Open Then
connect.Close()
End If
End Try
Return bSuccess
End Function

|||I'd recommend using SCOPE_IDENTITY. check docs for more info about SCOPE_IDENTIY and @.@.IDENTITy..


insert into Cl_Contacts (
Department,
Title,
Phone1,
Phone2,
Phone3,
Ext1,
Ext2,
Ext3,
Fax,
Email
)
values (
@.Department,
@.Title,
@.Phone1,
@.Phone2,
@.Phone3,
@.Ext1,
@.Ext2,
@.Ext3,
@.Fax,
@.Email
)

SELECT @.contactid = SCOPE_IDENTITY()

hth

Monday, March 12, 2012

How To Get Rid Of Sql Log

Hi,
I have a Data base on Sql Server 2000, sp3a. Database is used to to build summary tables to show reports on web. There are many sub systems, which prepare summarized tables for faster web page serving. Every thing is working fine.

The problem is sql server log gets full. In one given day it could go up to 18 GB. I know, if I back it up it gets truncated. Is there a way, I can stop sql server not to log, as it is not needed?

Can I set up Sql server to automatically truncate it. I do have auto shrink options checked, but log file still keeps growing.

Any idea, what is going on here.
Thanks,
Mashro::Is there a way, I can stop sql server not to log, as it is not needed?

You err, it IS needed.

Read up in the documentation what the log is used for. It is needed.|||Back it up regularly to keep it from growing out of control. You can't eliminate the log. It's a pretty key piece of the puzzle. If you have the db set to simple recovery, it should truncate the log on each checkpoint. Probably no need to use a full recovery model on a 'derived' reporting database like you seem to have.