473,756 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best methods to INSERT and UPDATE?

Max
I'm looking for best methods in terms of performance and simplicity to do an
INSERT and UPDATE. Using Microsoft's Application Blocks and SQL Server
stored procedures, this is simple:

Insert:
iNewCustomerID = Convert.ToInt32 (SqlHelper.Exec uteScalar(strCo nn, _
"myStoredPr oc", _
param1, _
param2, _
param3
.....))

Update:
Same idea, but use SqlHelper.Execu teNonQuery

THE PROBLEM:
When I insert large tables like a "customers" table that has 50+ fields, now
my stored procedure becomes VERY large and harder to maintain.

Does anyone have any tips on making this easier? My stored proc just seems
way out of hand now (see below). Is there a way I can at least trim it
down?

Thanks!

Max

------- working stored proc that returns the new id --------------
CREATE PROCEDURE InsertCustomers
(
@discountid int,
@email varchar,
@password varchar,
@bill_company varchar,
@bill_firstname varchar,
@bill_lastname varchar,
@bill_add1 varchar,
@bill_add2 varchar,
@bill_city varchar,
@bill_state varchar,
@bill_postal varchar,
@bill_prov varchar,
@bill_country varchar,
@bill_phone varchar,
@bill_fax varchar,
@ship_company varchar,
@ship_firstname varchar,
@ship_lastname varchar,
@ship_add1 varchar,
@ship_add2 varchar,
@ship_city varchar,
@ship_state varchar,
@ship_postal varchar,
@ship_prov varchar,
@ship_country varchar,
@ship_phone varchar,
@ship_fax varchar,
@maillist bit,
@hearaboutus varchar,
@comment varchar,
@created datetime,
@lastlogin datetime,
@logincount int,
@cc_name varchar,
@cc_number varchar,
@cc_expires_mon th varchar,
@cc_expires_yea r varchar,
@cc_cvn varchar,
@ip varchar,
@active bit,
@hearaboutus_ot her varchar,
@check_number varchar,
@browser_type varchar,
@entry_person varchar
)
AS

INSERT INTO customers
(
discountid,
email,
[password],
bill_company,
bill_firstname,
bill_lastname,
bill_add1,
bill_add2,
bill_city,
bill_state,
bill_postal,
bill_prov,
bill_country,
bill_phone,
bill_fax,
ship_company,
ship_firstname,
ship_lastname,
ship_add1,
ship_add2,
ship_city,
ship_state,
ship_postal,
ship_prov,
ship_country,
ship_phone,
ship_fax,
maillist,
hearaboutus,
comment,
created,
lastlogin,
logincount,
cc_name,
cc_number,
cc_expires_mont h,
cc_expires_year ,
cc_cvn,
ip,
active,
hearaboutus_oth er,
check_number,
browser_type,
entry_person
)
VALUES
(
@discountid,
@email,
@password,
@bill_company,
@bill_firstname ,
@bill_lastname,
@bill_add1,
@bill_add2,
@bill_city,
@bill_state,
@bill_postal,
@bill_prov,
@bill_country,
@bill_phone,
@bill_fax,
@ship_company,
@ship_firstname ,
@ship_lastname,
@ship_add1,
@ship_add2,
@ship_city,
@ship_state,
@ship_postal,
@ship_prov,
@ship_country,
@ship_phone,
@ship_fax,
@maillist,
@hearaboutus,
@comment,
@created,
@lastlogin,
@logincount,
@cc_name,
@cc_number,
@cc_expires_mon th,
@cc_expires_yea r,
@cc_cvn,
@ip,
@active,
@hearaboutus_ot her,
@check_number,
@browser_type,
@entry_person
)
SELECT CAST(@@IDENTITY AS INTEGER)
GO
Nov 18 '05 #1
1 1176
Nope! unless you can trim out fields that aren't going to be
updated/inserted ever. Here's some things I've found that make this a
little easier.

The best way I've found to manage big statements like you have is to use
unix utilities like gawk and sed to automatically generate some of this
stuff, for a certain input text file. Like for example in an input file, I
have fieldnames, fieldtypes, lengths, etc (which by the way you can get out
of a sql query - which I pasted below), then I have awk/sed scripts that run
off this file to create everything I need to access the table. I've even
gone as far as automatically creating sql parameters for insertion into
code. This is very powerful to be able to do this, but you won't figure it
out in 2 minutes, it takes some time to realize what you can do with these
tools. Also too, you don't have to use these unix utilities, you can create
a program in the language you are comfortable with to generate this stuff.

Hope this helps too, it took me awhile to figure this one out, but this is
some sql to get this information out of sql server.

select
c.table_name as TableName,
c.column_name as ColumnName,
p.value as ColumnDescripti on,
c.data_type as DataType,
c.character_max imum_length as DataTypeSize
from information_sch ema.columns as c
inner join dbo.sysusers as u on u.name = c.table_schema
inner join dbo.sysobjects as o on o.name = c.table_name and o.uid = u.uid
left outer join dbo.sysproperti es as p on p.id = o.id and p.smallid =
c.ordinal_posit ion
Where c.table_name = 'myTableName'

--Michael

"Max" <ma*****@portvi sta.com> wrote in message
news:F2******** ************@tw ister.tampabay. rr.com...
I'm looking for best methods in terms of performance and simplicity to do an INSERT and UPDATE. Using Microsoft's Application Blocks and SQL Server
stored procedures, this is simple:

Insert:
iNewCustomerID = Convert.ToInt32 (SqlHelper.Exec uteScalar(strCo nn, _
"myStoredPr oc", _
param1, _
param2, _
param3
....))

Update:
Same idea, but use SqlHelper.Execu teNonQuery

THE PROBLEM:
When I insert large tables like a "customers" table that has 50+ fields, now my stored procedure becomes VERY large and harder to maintain.

Does anyone have any tips on making this easier? My stored proc just seems
way out of hand now (see below). Is there a way I can at least trim it
down?

Thanks!

Max

------- working stored proc that returns the new id --------------
CREATE PROCEDURE InsertCustomers
(
@discountid int,
@email varchar,
@password varchar,
@bill_company varchar,
@bill_firstname varchar,
@bill_lastname varchar,
@bill_add1 varchar,
@bill_add2 varchar,
@bill_city varchar,
@bill_state varchar,
@bill_postal varchar,
@bill_prov varchar,
@bill_country varchar,
@bill_phone varchar,
@bill_fax varchar,
@ship_company varchar,
@ship_firstname varchar,
@ship_lastname varchar,
@ship_add1 varchar,
@ship_add2 varchar,
@ship_city varchar,
@ship_state varchar,
@ship_postal varchar,
@ship_prov varchar,
@ship_country varchar,
@ship_phone varchar,
@ship_fax varchar,
@maillist bit,
@hearaboutus varchar,
@comment varchar,
@created datetime,
@lastlogin datetime,
@logincount int,
@cc_name varchar,
@cc_number varchar,
@cc_expires_mon th varchar,
@cc_expires_yea r varchar,
@cc_cvn varchar,
@ip varchar,
@active bit,
@hearaboutus_ot her varchar,
@check_number varchar,
@browser_type varchar,
@entry_person varchar
)
AS

INSERT INTO customers
(
discountid,
email,
[password],
bill_company,
bill_firstname,
bill_lastname,
bill_add1,
bill_add2,
bill_city,
bill_state,
bill_postal,
bill_prov,
bill_country,
bill_phone,
bill_fax,
ship_company,
ship_firstname,
ship_lastname,
ship_add1,
ship_add2,
ship_city,
ship_state,
ship_postal,
ship_prov,
ship_country,
ship_phone,
ship_fax,
maillist,
hearaboutus,
comment,
created,
lastlogin,
logincount,
cc_name,
cc_number,
cc_expires_mont h,
cc_expires_year ,
cc_cvn,
ip,
active,
hearaboutus_oth er,
check_number,
browser_type,
entry_person
)
VALUES
(
@discountid,
@email,
@password,
@bill_company,
@bill_firstname ,
@bill_lastname,
@bill_add1,
@bill_add2,
@bill_city,
@bill_state,
@bill_postal,
@bill_prov,
@bill_country,
@bill_phone,
@bill_fax,
@ship_company,
@ship_firstname ,
@ship_lastname,
@ship_add1,
@ship_add2,
@ship_city,
@ship_state,
@ship_postal,
@ship_prov,
@ship_country,
@ship_phone,
@ship_fax,
@maillist,
@hearaboutus,
@comment,
@created,
@lastlogin,
@logincount,
@cc_name,
@cc_number,
@cc_expires_mon th,
@cc_expires_yea r,
@cc_cvn,
@ip,
@active,
@hearaboutus_ot her,
@check_number,
@browser_type,
@entry_person
)
SELECT CAST(@@IDENTITY AS INTEGER)
GO

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2237
by: a-ok | last post by:
Hi, My client has a product database od around 20000 items. And it is updated every few days when he gets a catalog from the supplier. It's supposed to work like this: if there already is a product in the database with that ID (primary key), just UPDATE the price, and if there is no product with that ID it should do an INSERT for that item. Now I've tried it in several ways and I can't seem to get it to execute
16
3036
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
16
1914
by: LP | last post by:
Hi, Every morning a .NET application downloads a file with cumulative data which needs to be appended to SQL Server table. This program needs to identify records that have not been previously inserted (there's a unique identifier field) and only insert those. Also I must reuse our class that does updates, it basically can update any table by using SqlDataAdapter .update method. So I have to rule out bulk inserts, DTS, etc... I think I...
9
5492
by: Alfred Taylor | last post by:
I'm testing the waters of n-tier development and I ran into a scenario that I'm not sure what the best solution would be. I have a Company object which contains a collection of contacts retrieved from a database. In the presentation layer, the user will be able to add/delete/modify this collection in which case it needs to be synced with the database. The question is basically how best to do this? Aside from overriding the add/remove...
0
4247
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com. --------------------------------------------------------------------------------
6
1637
by: Nate | last post by:
I am in a slight predicament trying to determine the most efficient and effective way to connect/disconnect from a database within a business object (c# dll). I'm also keeping in mind the concept of connecting late and disconnecting early. Background: - multi-tier application (code-behind uses properties and methods of the business object, the business object handles the data layer) For instance (in an ASPX code-behind file):
2
1519
by: Fred Flintstone | last post by:
What's the difference between these two methods? 1 - Parameterrized SQL queries: Dim CommandObject As New Data.SqlClient.SqlCommand With CommandObject .Connection = myConnection .Parameters.Clear() .Parameters.Add("@TextField", SqlDbType.NVarChar, 50).Value = TextField
7
3583
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id - FirstName - LastName - CompanyId (many-to-one )
4
1530
by: trullock | last post by:
Hi, Can anyone suggest the best way to go about the following... I'm tracking clicks (mouse down x,y coordinates) on a web page by using some javascript to create an XHR which sends the coordinates to a recording service, such as: /Record.ashx?X=123&Y=456
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.