Friday, June 8, 2012

How to return the ID from the database on insert

Here is the Example of a Insert statement which returns ID of the row got inserted.

DECLARE ID int
INSERT INTO <tablename> (<colums name>) VALUES (<values>)
SELECT ID = SCOPE_IDENTITY()


Hope it will help you.

Thanks
Solomon S.


If information is helpful then don't forget to post comments

Selecting Random Rows in SQL Server


Here is the quick and easy way to select random rows in sql server.

SELECT <columns Name> FROM <table name> ORDER BY NewID()

Reference from : Brian Jeremy
......

Hope it will help you.

Thanks
Solomon S.


If information is helpfull then don't forget to post comments

Tuesday, June 5, 2012

How to stop bulk update in sql server

I was searching how to prevent SQL Injection and stop updating all rows of the table and I got a solution in given link http://www.mssqltips.com/sqlservertip/1851/prevent-accidental-update-or-delete-commands-of-all-rows-in-a-sql-server-table/

Post is really good and helpfull, But when I was Creating Trigger to stop bulk update by following the link I found index =1 is not working for few tables then i used index=0 still not for all tables finally I make some changes in SQL and got the result.

I removed Index from where clause and finally its work for all tables.

My Modified Query is given below.

USE AdventureWorks
GO
CREATE TRIGGER [Purchasing].[uPreventWholeUpdate] 
ON [Purchasing].[VendorContact] 
FOR UPDATE AS 
BEGIN
     DECLARE @Count int
     SET @Count = @@ROWCOUNT;
         
     IF @Count >= (SELECT SUM(row_count)
         FROM sys.dm_db_partition_stats 
         WHERE OBJECT_ID = OBJECT_ID('Purchasing.VendorContact' ) )
     BEGIN
         RAISERROR('Cannot update all rows',16,1) 
         ROLLBACK TRANSACTION
         RETURN;
     END
END
GO
 
 
If this link is help full please do post comments.

Start writting blog from today

Hi friends,

I have planned to write blog when I'll get time. and I thought if I'll will start writing on Programming and new Technology that I learned and want to share, That will help me later and the user also. And new developers can also get ideas and solutions from my post. isn't it ?

I planned to write on ASP.Net, PHP, SQL Server, AJAX, JQuery.