Mark Rae said:
SQL Server doesn't properly support array parameters, though you can look
at XML data types if you want to be really clever. I tend not to bother
with this, as the method below is so much easier.
1) Walk through all of the rows in the GridView and construct a
comma-delimited string of the IDs of those which are checked. e.g.
"234,236,237"
2) Create a stored procedure like below and pass the above string to it:
CREATE PROCEDURE MyUpdateSP
@pstrCheckedIDs varchar(4000)
AS
DECLARE @strSQL nvarchar(4000)
SET @strSQL = 'UPDATE MyTable SET MyBitField = 1 WHERE MyIdField IN(' +
@pstrCheckedIDs + ')'
--PRINT @strSQL
UPDATE MyTable SET MyBitField = 0
EXEC sp_executesql @strSQL
Rem in the PRINT line and rem out the last two lines if you want to see
what the dynamic SQL looks like first for testing purposes.
You will almost certainly want to make the above a bit more robust by
surrounding the two write statements in a transaction. This wouldn't be a
very efficient solution if the table contained many rows and you were
updating only a handful of them, but it demonstrates the technique at
least...
Thanks again.
I've had to add a WHERE clause to include the indexed OrganizationDomain
field else it would be too slow:
SET @strSQL = 'UPDATE tblSubscription SET RenewObtain = 1 WHERE (CounterID
IN(' +
@pstrCheckedIDs + ') AND
OrganizationDomain='+@strCurrentOrganizationDomain+')'
There are however two issues with doing that:
1) It's giving an error because it's using OrganizationDomain=ABCCompany.com
instead of OrganizationDomain='ABCCompany.com'
2) It's subject to SQL Injection attack so I'd need to use a parameter
instead of using the result of the @strCurrentOrganizationDomain parameter
'soft-coded' in there.
Do you know what syntax change I'd need to make to the SET @strSQL statement
to allow for such a parameter?
Also, on another issue, I think I'd need to change:
"234,236,237"
to
"A234,A236,A237"
otherwise the IN function would find a 2 even though a 2 was not in the list
of numbers.
So would I use:
WHERE ('A'+MyIdField) IN(' + @pstrCheckedIDs + ')'
?