|
In Sql Server from version 2000 onwards there is a feature called an INSTEAD OF trigger that you can use. To use it, you create a copy of your table as a view and use this as the target of your inserts. Behind this view, create a trigger with the signature INSTEAD OF INSERT where you would normally put the FOR INSERT.
The trigger would look something like this:
CREATE TRIGGER InsertIntoSummarySells
INSTEAD OF INSERT
AS
BEGIN
DECLARE @C1 INT -- or whatever type it is
DECLARE @C2 INT -- ...
... -- And so on, with the parameters
SET NOCOUNT ON
UPDATE tblSummarySells
SET C5 = @C5,
C6 = @C6,
C7 = @C7
WHERE
C1 = @C1 AND C2 = @C2 AND C3 = @C3 AND C4 = @C4
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO tblSummarySells ....
END
END Now, the advantage of this approach is that anytime you do an insert onto the view, this code will run so it should always behave consistently - rather than having to remember to call the stored procedure to perform the update.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I am getting following exception in the code below
Exception occurs when closing connection,
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The SqlCommandTimeout is already set 0 as you can see in the code below,
When i search records for first time it works fine, but the exceptions start coming second time or third time onwards,..
Protected Overrides Sub DataPortal_Fetch(ByVal criteria As Object)
' Load object data from database.
Dim crit As criteria = DirectCast(criteria, criteria)
Dim con As New SqlConnection(DB())
// db string is something like
//"data source=hurst\SQL2000_SP4;initial catalog=MYDB_OOP_11;Connection Timeout=60;user=asd_user;password=asd_user;"
Dim cmd As New SqlCommand
con.Open()
Try
With cmd
.Connection = con
.CommandType = CommandType.StoredProcedure
.CommandTimeout = 0
' Set up and call appropriate stored procedure.
If crit.IsSimpleList Then
.CommandText = DBObject & "_info_simple_get"
With .Parameters
// some code for adding params for SP
End With
Else
.CommandText = DBObject & "_info_get"
With .Parameters
// adding param for Stored Procedure here
End With
End If
Dim dr As New SafeDataReader(.ExecuteReader)
Try
Me.Fetch(dr, crit.IsSimpleList)
// The fectch function loops for each read of datareader and creates a new object, passing it dr object, each object then reads its column from dr
Finally
dr.Close()
End Try
End With
Finally
con.Close() // I get exception here..on closing..
End Try
End Sub
Is it something because of improper use of DataReader ? or what is it, I really couldnt figure out, I have Tried using GC.collect() etc , but no use,
please help
Ahmed
Ahmed
|
|
|
|
|
U can Set ur own time for the connection and try.....
If U Get Errors U Will Learn
If U Don't Get Errors U Have Learnt
|
|
|
|
|
I would suspect that your problem is more to do with the connection timeout you are using in your connection string.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi
I use the following code to add a row to my database using ADO.NET in VB.NET:
Dim strConnstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DB.mdb"
Dim Sql As String = String.Format("INSERT INTO Employee(FName,LName,Salary) VALUES('{0}','{1}',{2})", txtFirstName.Text, txtLastName.Text, txtSalary2.Text)
Dim Conn As New OleDbConnection(strConnstring)
Dim Comm As New OleDbCommand(Sql, Conn)
Dim DA As New OleDbDataAdapter(Comm)
Dim DS As New DataSet
Conn.Open()
Comm.ExecuteNonQuery()
Conn.Close()
Everything looks fine, the datagrid which shows Employee table, shows that the item is added.
But DataAdapter does not update the dataset! I noticed that when I add a row by this method, the State of the row is not set to Added, like when I use Rows.Add (which works just fine!)
I need to do this using this SQL command, but how to update the database also?
(I again notice that myDataSet.GetChanged is null!)
|
|
|
|
|
Hi,
I have configured MAPI profile using POP3 and the same profile has been configured in SQL server 2000 in SQL server Agent(Management) and in SQL Mail(Support Services).Mail is sent successfully when we compose it manually through Microsoft office outlook.The problem is that when i execute XP_sendmail stored procedure is executing fine and the output is Mail Sent.But the mail is remaining in outlook itself.When i open the outlook Send/Receive is happening and the mail is sent successfully.Unless i open outlook Iam unable to send the mail.Please suggest me what would be the possible solution for this.Any help from you side is appreciated.Thanks in Advance.
regards,
Shaik.
|
|
|
|
|
My only advice is not to use SQL Server 2000 built-in mail capabilities - consider using blat.exe[^].
This[^] may be helpful.
|
|
|
|
|
Hi,
I need to bring back records in a certain date range.
I need it to be from May 2007 to August 2007. I can pass through (2007 and 5) and (2007 and 8) but not sure how my WHERE will look like.
Please can some one help??
Regards
ma se
|
|
|
|
|
searching on datetime column for dates between May-2007 and Aug-2007
DECLARE @Mth1 varchar(5)
DECLARE @Yr1 varchar(5)
DECLARE @Mth2 varchar(5)
DECLARE @Yr2 varchar(5)
SET @Mth1 = '5'
SET @Yr1 = '2007'
SET @Mth2 = '8'
SET @Yr2 = '2007'
DtCol >= Convert(datetime, '01/' + @Mth1 + '/' + @yr1, 103)
AND DtCol < DateAdd(m, 1, Convert(datetime, '01/' + @Mth2 + '/' + @yr2, 103))
here the condition is starting from 1st of May-2007 to 31st of Aug-2007
i.e. deriving both dates initial date would be 1st of the month and ending date is less than 1st of required month + 1
Regards
KP
|
|
|
|
|
Dear All ,
I am trying to parse a huge text files(size: 50kb to 1MB).
Currently I have developed a mechanism using regular expressions which
can group the values in each and every line, finally the values are
formatted into a XML string which is passed into a stored procedure
where the XML is loaded and again by using OPENXML statement I read
all the values and records are inserted into the tables. My problem
here is with the execution time of the Stored Procedure which is
taking nearly 45 secs to execute the XML string.
Row Count ranges from 100 to 2000.
Column count is 40.
Can anyone please suggest me an alternate method or the necessary step
which I can do with the front end and back end.
Thanks and Regards,
Valan.
|
|
|
|
|
|
I have a limitation in using the BULK INSERT or BCP,
It is a text file of unicode characters with txt extension.
Actual file information will start from
fifth line and it will end before the last second line.
Business logic has to be implemented like validating the
format of file and validating the values of the field,
due to these type of requirements I prefered to apply
regular expressions from front end and formatted values
are converted into XML strings.
I 'am using SQL Server 2000, and .NET 2.0
Can you please suggest me with better method to follow.
Thanks
Valan.
|
|
|
|
|
How about, instead of converting data to XML, changing the TXT file format (and performing all the necessary validation), so bcp /BULK INSERT can process it?
|
|
|
|
|
Mr. Chopeen,
Are you sure by using the BULK INSERT I will be able to reduce the
execution time, then I will try in all the possible ways to use
the BULK INSERT to complete my File Parsing.
Is that only the better and best way for inserting records into the
DataBase?
Please suggest me.
Thanks and Regards
Valan
|
|
|
|
|
A completely different idea just came to my mind - you are using some kind of .NET application to generate the XML file, aren't you? Can't you change it and generate INSERT statements? Then you will have to simply execute those INSERT-s.
|
|
|
|
|
Ok Fine thanks I will try to do with your suggestion.
But any Idea why it has to take a longer time for XML
execution.
Thanks and Regards
Valan
|
|
|
|
|
I don't have much experience with XML so I'm not sure I will be able to help, but if you post your stored procedure I'll take a look.
|
|
|
|
|
This is my mail ID tonic_valan@hotmail.com,
if you are willing I can mail my SP.
|
|
|
|
|
I have recently been using sql server 2005 and cannot find how you set the default directory to save your sql scripts. This was in the options and called "Query File Directory" in sql 2000.
Thanks, Jared
|
|
|
|
|
hi
i have developed some t-sql to backup/restore any database.
while restoring a database, if any session or connection be active on it i can not restore database and recieve exception.
How can i restore such databases?
How can i kill all sessions from database to solve my problem?
|
|
|
|
|
WDI wrote: How can i kill all sessions from database to solve my problem?
That would not be nice for your users. Imagine, you are working along and wham database session is killed and you lose work.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
|
|
|
|
|
I second that, those type of tasks are usually scheduled for when people are not working.
www.codewash.com
|
|
|
|
|
The only way I can think to end all the sessions would be by stopping the SQL service and then restarting it, your users might open their session again by the time you have started restoring though in which case it will still fail.
You could just run the command "NET STOP MSSQLSERVER" and then "NET START MSSQLSERVER" within your application.
There probably is a better way though but like the others said, it's probably not a good idea.
There are 10 types of people in the world, those who understand binary and those who dont.
|
|
|
|
|
You can put the database into a mode where only a single connection is allowed at a time by using ALTER DATABASE db SET SINGLE_USER . Use the WITH ROLLBACK option to tell SQL Server when to rollback/abort any other existing connections. The connection you run ALTER DATABASE from remains connected.
You may also be able to restore the database if you take it OFFLINE .
|
|
|
|
|
I am trying to insert Data in SQL Database using C#, it's not giving any error but at the same time no rows are getting updated in my database.
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=sbs;database=bansal;Trusted_Connection=Yes");
SqlCommand myCommand = new SqlCommand("InsertCategory", myConnection);
myCommand.CommandText = "InsertCategory";
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@CategoryName",SqlDbType.NChar, 15));
myCommand.Parameters["@CategoryName"].Value = "Ambarish";
myCommand.Connection.Open();
try
{
myCommand.ExecuteNonQuery();
}
catch(SqlException e){}
myCommand.Connection.Close();
}
Following is the Stored Procedure:
CREATE PROCEDURE InsertCategory
(
@CategoryName nchar(15)
)
AS
INSERT INTO Categories (CategoryName) VALUES(@CategoryName)
Table Name is Categories and has two Columns only:
1. CategoryName NChar 15
2. Identity Int 4 (Identity Seed is 1 starting from 1)
Sachin Jain
-- modified at 4:36 Saturday 4th August, 2007
|
|
|
|