PC Review


Reply
Thread Tools Rate Thread

RE: Passing checked items into a sql statement

 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      22nd Jan 2007
Hello Michael,

From your description, you're developing a data access ASP.NET page which
will use Gridview to let users select some certain records to update, and
when performing the udpate, you will need to dynamically put all those
selected GridView Row(keys) into your SQL query statement (in the "in
(.....)" block), correct?

Based on my experience, for such scenario, you need to use dynamic SQL
execution. Fixed compiled store-procedure can not fully meet the
requirement here. For dynamic SQL Execution, you have the following options
here:

1. programmatically use string concatenate in your .net data access code to
build the SQL query statement(includes all the items choosed in gridview)
and then execute it through SQLCommand.

2. If you have other paramters in addition to the dynamic key list and you
still want to use a stored-procedure, you can create a stored-procedure
with an additional parameters, and provide a VarChar parameter which used
to hold the var-length key list(such as "('key1','key2',....)" ). And you
can concatenate these expression as a complete SQL statement in your
store-procedure and use "sp_executesql").

#Using sp_executesql
http://msdn2.microsoft.com/en-us/library/ms175170.aspx

BTW, for such dynamic SQL execution scenario, using stored procedure won't
help much on performance.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      23rd Jan 2007
Thanks for your reply Michael,

SQL Server version is not a limitation here, both SQL Server 2000 an 2005
support sp_executesql(actually most modern DBMS should support such dynamic
sql execution mechanism).

The reason why I said for dynamic SQL(sql statement be determined at
runtime dynamically before execute) is as below:

** One of the big advantage of using Stored Procedure is it can help
precompile the sql statement and avoid parse and compile the SQL statement
at server-side every time we execute it.

** However, for sp_executeSql, it help execute sqlstatement which is
dynamically built. The SQL Server can not precompile it in advance. So when
we use stored-procedure to execute dynamic SQL, the server engine still
need to compile and parse the "dynamic sql" everytime we execute it. That's
why using SP or not doesn't matter so much for dynamic sql execution.

If you have any other question or anything we can help, please feel free to
let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Only Show Checked Items Jesss Microsoft Excel Misc 1 8th Sep 2008 11:17 PM
Checked items in checkedlistbox gets unchecked diego Microsoft VB .NET 0 29th Mar 2007 03:28 AM
What's a good way to "run all checked items in a checked listbox"? sherifffruitfly Microsoft C# .NET 1 8th Nov 2006 10:30 PM
Passing items from drown list into the where statement of a query =?Utf-8?B?ZnRydWppbGww?= Microsoft Access VBA Modules 3 7th Dec 2005 01:27 PM
CheckedListBox - Get the value of checked items =?Utf-8?B?UmFt?= Microsoft C# .NET 4 15th Jul 2004 05:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:21 PM.