Friday, March 9, 2012
Inserts to tables as one or two users.
Background: C++ program accesses SQL via ODBC doing
massive inserts to two different tables. The data goes
either to one table or the other, but not both.
Problem: Is it more efficient to have the program access
the database as one or two SQL users?
1. user DOG inserts to table XXX "and" table YYY.
or
2. user DOG inserts to table XXX,
user CAT inserts to table YYY?
Thanks for your help,
DonUnless you need to load them in parallel there isn't any reason to use two
users. Adding connections has a slight overhead that you probably don't
need for this type operation.
--
Andrew J. Kelly
SQL Server MVP
"Don" <ddachner@.yahoo.com> wrote in message
news:0bf401c38126$d4174120$a001280a@.phx.gbl...
> SQL 7.0 SP4
> Background: C++ program accesses SQL via ODBC doing
> massive inserts to two different tables. The data goes
> either to one table or the other, but not both.
> Problem: Is it more efficient to have the program access
> the database as one or two SQL users?
> 1. user DOG inserts to table XXX "and" table YYY.
> or
> 2. user DOG inserts to table XXX,
> user CAT inserts to table YYY?
> Thanks for your help,
> Don|||no, it wouldn't make any difference in performance, it
will only increase work for you to manage permissions to
two users.
Also, consider using stored procedures rather than direct
insert statements. this will give slight performance gain,
and better management of sql code.
>--Original Message--
>SQL 7.0 SP4
>Background: C++ program accesses SQL via ODBC doing
>massive inserts to two different tables. The data goes
>either to one table or the other, but not both.
>Problem: Is it more efficient to have the program access
>the database as one or two SQL users?
>1. user DOG inserts to table XXX "and" table YYY.
>or
>2. user DOG inserts to table XXX,
> user CAT inserts to table YYY?
>Thanks for your help,
>Don
>.
>
Inserts performance
I'm trying to optimize a process that synchronizes some proprietary objects to a database. Currently, this synchronization is made via ADO by using SQL statements. There are two differents actions that are made: the first initialization is made by a lot of INSERTs into different tables and the second operation is inserting/updating/removing objects one by one when it has changed.
Our objects are stored across multiple tables because they have a hierarchical structure.
My question is about performance for the first operation : what is the best way to achieve multiple inserts across multiple tables in the shortest time. We actually use ADO and an INSERT statement for each row which is obviously not the preferred method.
Thanks.One method you can try is:: Define cluster indexes on each table such that new inserts are distributed on multiple pages. Not only on last page.
Multiple inserts on last page will slow down ur speed.|||Thanks, but i was thinking about client methods to improve the performance of inserts such as batches. But i've got no knowledge of advanced methods. The actual problem is that one request is sent by INSERT of a row and we have roughly 1 million inserts to do.
Originally posted by avneesh
One method you can try is:: Define cluster indexes on each table such that new inserts are distributed on multiple pages. Not only on last page.
Multiple inserts on last page will slow down ur speed.|||1. SET NOCOUNT ON
removes useless "(1 row(s) affected)" messages (server feedback)
2. Create SPs for the insert
SPs are precompiled and therefore faster.
Insertion data via Stored Procedure [URGENT]!
Question:
=======
Q1) How can I insert a record into a table "Parent Table" and get its ID (its PK) (which is an Identity "Auto count" column) via one Stored Procedure??
Q2) How can I insert a record into a table "Child Table" whose (FK) is the (PK) of the "Parent Table"!! via another one Stored Procedure??
Example:
----
I have two tables "Customer" and "CustomerDetails"..
SP1: should insert all "Customer" data and return the value of an Identity column (I will use it later in SP2).
SP2: should insert all "CustomerDetials" data in the record whose ID (the returned value from SP1) is same as ID of the "Customer" table.
FYI:
--
MS SQL Server 2000
VS.NET EA 2003
Win XP SP1a
VB.NET/ASP.NET :)
Thanks in advanced!There are a couple of ways to get the last inserted IDENT, but I prefer to use IDENT_CURRENT('table_name') to get the ID from the last inserted record of thespecified table.
|||Ok, what about if I want to do these process in two SPs??
INSERT INTO Customer (val1,val2,etc) VALUES (@.Val1,@.val2,etc)INSERT INTO CustomerDetails (ID,Val1,Val2, etc) VALUES ((IDENT_CURRENT('Customer'),@.val1,@.val2,etc)
I want to take the IDDENTITY value from the fitrst "INSERT INTO" statment, because I need this value in my source code as well as other Stored Procedure(s).
Thanks in advanced!|||Insert Blah...;
Return SCOPE_IDENTITY()|||Thanks gays.
I think my problem is how to get the returned value (IDDENTITY value) from the VB.NET code (in other words, how to extract it from VB.NET/ADO.NET code)?
Note:
--
VB.NET/ADO.NET
or
C#.NET/ADO.NET
Are are ok, if you would like to demonstrate your replay. ( I want the answer!).
Thanks again.|||YES!!
The problem was from my ADO.NET part.
Thanks for your help gays.
Wednesday, March 7, 2012
Inserting via a view
the environemnts and the code. Both environments are identical however,
service packs a different.
Environment 1: This environmnet the insert works
Microsoft SQL Server 2000 - 8.00.760 (Intel X86)
Windows 2k sp4
Environment 2: This envirnoment the insert fails
Microsoft SQL Server 2000 - 8.00.818 (Intel X86)
Here is the insert statment;
INSERT INTO vMoxyStandardRestriction ( R.RestrictionID,
R.RestrictionType,
R.SecAssocType,
R.RestrictionName,
R.CanOverride,
D.TranCodeBits,
D.MinCompRestPercent,
D.MaxCompRestPercent,
D.IsAggregate,
D.RoundOption,
D.RoundFactor,
D.MinShares,
D.MinValue,
D.CompType)
VALUES (11, 1, 16, 'Unreconciled', 1, 12, 0.000000000000000e+000,
0.000000000000000e+000, 0, 0, 0.000000000000000e+000, 0.000000000000000e+000
,
0.000000000000000e+000, 0)
Here is the code to the view
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
ALTER VIEW vMoxyStandardRestriction AS
SELECT R.RestrictionID, R.RestrictionType, R.SecAssocType,
R.RestrictionName, R.CanOverride,
D.TranCodeBits, D.MinCompRestPercent, D.MaxCompRestPercent, D.IsAggregate,
D.RoundOption, D.RoundFactor, D.MinShares, D.MinValue, D.CompType
FROM MoxyRestriction R, MoxyRestDef D
WHERE R.RestrictionID = D.RestrictionID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GOThis is the error message in the environment which does not work.
Server: Msg 4405, Level 16, State 2, Line 1
View or function 'vMoxyStandardRestriction' is not updatable because the
modification affects multiple base tables.
"FredG" wrote:
> Hello All, I have an strange issue regarding inserts via a view. Below are
> the environemnts and the code. Both environments are identical however,
> service packs a different.
> Environment 1: This environmnet the insert works
> Microsoft SQL Server 2000 - 8.00.760 (Intel X86)
> Windows 2k sp4
> Environment 2: This envirnoment the insert fails
> Microsoft SQL Server 2000 - 8.00.818 (Intel X86)
> Here is the insert statment;
> INSERT INTO vMoxyStandardRestriction ( R.RestrictionID,
> R.RestrictionType,
> R.SecAssocType,
> R.RestrictionName,
> R.CanOverride,
> D.TranCodeBits,
> D.MinCompRestPercent,
> D.MaxCompRestPercent,
> D.IsAggregate,
> D.RoundOption,
> D.RoundFactor,
> D.MinShares,
> D.MinValue,
> D.CompType)
> VALUES (11, 1, 16, 'Unreconciled', 1, 12, 0.000000000000000e+000,
> 0.000000000000000e+000, 0, 0, 0.000000000000000e+000, 0.000000000000000e+0
00,
> 0.000000000000000e+000, 0)
> Here is the code to the view
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS OFF
> GO
> ALTER VIEW vMoxyStandardRestriction AS
> SELECT R.RestrictionID, R.RestrictionType, R.SecAssocType,
> R.RestrictionName, R.CanOverride,
> D.TranCodeBits, D.MinCompRestPercent, D.MaxCompRestPercent, D.IsAggreg
ate,
> D.RoundOption, D.RoundFactor, D.MinShares, D.MinValue, D.CompType
> FROM MoxyRestriction R, MoxyRestDef D
> WHERE R.RestrictionID = D.RestrictionID
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO|||Hi Fred
Your error messages seems pretty explicit. You cannot insert into a view
that is based on a join. If you could, your single insert would have to put
rows into both the underlying tables, and according to BOL:
[If you insert into a view] .. the modifications made by the INSERT
statement cannot affect more than one of the base tables referenced in the
FROM clause of the view. For example, an INSERT into a multitable view must
use a column_list that references only columns from one base table.
If you are not getting the message for one of your servers, that is the
weirdness. Perhaps you have an INSTEAD OF TRIGGER on the view on that
server.
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com
"FredG" <FredG@.discussions.microsoft.com> wrote in message
news:B1BE7DA5-30D1-41F7-8C63-E213DA147DFA@.microsoft.com...
> Hello All, I have an strange issue regarding inserts via a view. Below are
> the environemnts and the code. Both environments are identical however,
> service packs a different.
> Environment 1: This environmnet the insert works
> Microsoft SQL Server 2000 - 8.00.760 (Intel X86)
> Windows 2k sp4
> Environment 2: This envirnoment the insert fails
> Microsoft SQL Server 2000 - 8.00.818 (Intel X86)
> Here is the insert statment;
> INSERT INTO vMoxyStandardRestriction ( R.RestrictionID,
> R.RestrictionType,
> R.SecAssocType,
> R.RestrictionName,
> R.CanOverride,
> D.TranCodeBits,
> D.MinCompRestPercent,
> D.MaxCompRestPercent,
> D.IsAggregate,
> D.RoundOption,
> D.RoundFactor,
> D.MinShares,
> D.MinValue,
> D.CompType)
> VALUES (11, 1, 16, 'Unreconciled', 1, 12, 0.000000000000000e+000,
> 0.000000000000000e+000, 0, 0, 0.000000000000000e+000,
> 0.000000000000000e+000,
> 0.000000000000000e+000, 0)
> Here is the code to the view
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS OFF
> GO
> ALTER VIEW vMoxyStandardRestriction AS
> SELECT R.RestrictionID, R.RestrictionType, R.SecAssocType,
> R.RestrictionName, R.CanOverride,
> D.TranCodeBits, D.MinCompRestPercent, D.MaxCompRestPercent,
> D.IsAggregate,
> D.RoundOption, D.RoundFactor, D.MinShares, D.MinValue, D.CompType
> FROM MoxyRestriction R, MoxyRestDef D
> WHERE R.RestrictionID = D.RestrictionID
> GO
> SET QUOTED_IDENTIFIER OFF
> GO
> SET ANSI_NULLS ON
> GO
>
Friday, February 24, 2012
Inserting Records with limited privileges
access to the table (db_datawriter, db_denydatareader).
That way, if the server is ever compromised, the access information
stored in the source code's connection string will not allow anybody to
actually read the database.
The problem is that I would like to use ADO methods to insert the data
(to prevent SQL injections), but I can't seem to get the right
connection. It works in plain SQL, but I'd rather not use it.
My current code looks like this:
connection="Provider=SQLOLEDB.1;User ID=DBwriter;Password=XXX;Data
Source=MYSERVER;Initial Catalog=MYDB;"
set conn=server.createobject("ADODB.Connection")
conn.mode=2 ' adModeWrite
conn.open connection
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open "MYTABLE", conn, adOpenKeySet, adLockPessimistic, adCmdTable
rs.AddNew
rs.Fields("testfield") = "TESTDATA"
rs.Update
And the error I get is:
Microsoft OLE DB Provider for SQL Server (0x80040E09)
SELECT permission denied on object 'MYTABLE', database 'MYDB', owner
'dbo'.
(If I use a User with read privileges in the connection string
everything works fine.)Create a stored procedure to do this and then call the proc from ASP.
Stored procedures are a powerful way to create a secure data-access
layer because you can deny users all permissions on table objects and
just grant them execute permission on the procs you want them to use.
This approach also protects against SQL Injection, provided you avoid
dynamic code in your procs.
--
David Portas
SQL Server MVP
--|||(stacey.michols@.gmail.com) writes:
> My current code looks like this:
> connection="Provider=SQLOLEDB.1;User ID=DBwriter;Password=XXX;Data
> Source=MYSERVER;Initial Catalog=MYDB;"
> set conn=server.createobject("ADODB.Connection")
> conn.mode=2 ' adModeWrite
> conn.open connection
> Set rs = Server.CreateObject ("ADODB.Recordset")
> rs.Open "MYTABLE", conn, adOpenKeySet, adLockPessimistic, adCmdTable
> rs.AddNew
> rs.Fields("testfield") = "TESTDATA"
> rs.Update
>
> And the error I get is:
> Microsoft OLE DB Provider for SQL Server (0x80040E09)
> SELECT permission denied on object 'MYTABLE', database 'MYDB', owner
> 'dbo'.
> (If I use a User with read privileges in the connection string
> everything works fine.)
When you use adCmdTable, ADO reads the row in the table - or rather
it tries to do.
You should not use Open at all. Just send parameterized INSERT statements
with adCmdText:
cmd.CommandText = INSERT tbl(col1, col2) VALUES (?, ?)
cmd.CreateParameter("@.par1", ad..., ,, Value)
cmd.CreateParameter(...)
cmd.Execute ,,adExcuteDirect
Please see the ADO documentation for all details on CreateParameter.
You can also use stored procedure as suggested by David. In such case
you should use command type adCmdStoredProcedure. You would pass
parameters in the same way as in the snipped above. Stored Procedures
can prove to be a performance booster in your case.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks for the pointer towards stored procedures!
I just got into 'industrial strength' databases (coming from Access),
and after checking out all the things you can do with SPs, I'll NEVER
go back!
Inserting Records via Stored Procedure
I am trying to insert a record in a SQL2005 Express database. I can use the sp fine and it works inside of the database, but when I try to launch it via ASP.NET it fails...
here is the code. I realize it is not complete, but the only required field is defined via hard code. The error I am getting states it cannot find "sp_InserOrder"
===
ProtectedSub Button1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.Click
Dim connAs SqlConnection =Nothing
Dim transAs SqlTransaction =Nothing
Dim cmdAs SqlCommand
conn =New SqlConnection(ConfigurationManager.ConnectionStrings("PartsConnectionString").ConnectionString)
conn.Open()
trans = conn.BeginTransaction
cmd =New SqlCommand()
cmd.Connection = conn
cmd.Transaction = trans
cmd.CommandText ="usp_InserOrder"
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Parameters.Add("@.MaterialID", Data.SqlDbType.Int)
cmd.Parameters.Add("@.OpenItem", Data.SqlDbType.Bit)
cmd.Parameters("@.MaterialID").Value = 3
cmd.ExecuteNonQuery()
trans.Commit()
=====
I get an error stating cannot find stored procedure. I added the Network Service account full access to the Web Site Directory, which is currently running locally on Windows XP Pro SP2.
Please help, I am a newb and lost...as you can tell from my code...
Are you absolutely sure that your stored procedure is called usp_InserOrder. It would make more sense if it was called usp_InsertOrder.|||It is called that. That is what I was thinking at firs, but I checked the database and that is what I named it.|||I ran it inside of SQL 2005 here is the output...
(0 row(s) returned)
@.RETURN_VALUE = 0
Finished running [dbo].[InserOrder].
|||You code should read -cmd.CommandText ="InserOrder" as that is what it is called in the report you posted above.Sunday, February 19, 2012
INSERTING NULL VALUES VIA STORED PROCEDURES
Hi,
Becouse some of my stored procedure parameters can be NULL, for every parameter with potential NULL value I have to do checking like this:
errorParams[3].Value = (e.InnerException==null)?(object)DBNull.Value:(object)e.InnerException;
When I do it like this ( it is a part of preparing values for insert ):
errorParams[3].Value = e.InnerException
no row is added. Is there any way to pass over that check : (e.InnerException==null)?(
and do it easier?
This the sample exception message for that issue:
{System.Data.SqlClient.SqlException: Procedure or Function 'sp_addError' expects parameter '@.InnerException', which was not supplied.
Thanks,
Pawe?
That depends on your Procedure declaration, you will have to allow a default parameter in orde to let it work.CREATE PROCEDURE sp_addError
(
@.InnerException VARCHAR(1000) = NULL --Which declares the default if no value is passed through
)
(...)
HTH, Jens K. Suessmeyer.
http://www.sqlserver2005.de|||
Cool :) Big thanks!
Pawe?
|||Thank you very muchIt was solution of my Big problem
INSERTING NULL VALUES VIA STORED PROCEDURES
Hi,
Becouse some of my stored procedure parameters can be NULL, for every parameter with potential NULL value I have to do checking like this:
errorParams[3].Value = (e.InnerException==null)?(object)DBNull.Value:(object)e.InnerException;
When I do it like this ( it is a part of preparing values for insert ):
errorParams[3].Value = e.InnerException
no row is added. Is there any way to pass over that check : (e.InnerException==null)?(
and do it easier?
This the sample exception message for that issue:
{System.Data.SqlClient.SqlException: Procedure or Function 'sp_addError' expects parameter '@.InnerException', which was not supplied.
Thanks,
Pawe?
That depends on your Procedure declaration, you will have to allow a default parameter in orde to let it work.CREATE PROCEDURE sp_addError
(
@.InnerException VARCHAR(1000) = NULL --Which declares the default if no value is passed through
)
(...)
HTH, Jens K. Suessmeyer.
http://www.sqlserver2005.de|||
Cool :) Big thanks!
Pawe?
|||Thank you very muchIt was solution of my Big problem