Posts

Showing posts from April, 2013

Retrieving Comma separated values for a column instead of multiple rows

SELECT u.intUserId, ( SELECT  cast(Roles.vchrole AS varchar)+ ', ' FROM roles INNER JOIN user_roles ur ON ur.intRoleId=roles.intRoleId INNER JOIN users u1 ON u1.intUserId=ur.intUserId WHERE u.intUserId=u1.intUserId FOR XMl PATH ('') ) AS AllRoles FROM users  u GROUP BY u.intUserId Another Solution Below:   SELECT   @EmployeeIds   =   COALESCE ( @EmployeeIds   +   ',' ,   '' )   +   CAST ( EmployeeId   AS   VARCHAR ( 5 ))        FROM   Employees        WHERE   City   =   @City

Update table using joins and cases

UPDATE Property_New SET    PropertyNumber = ( CASE                  WHEN property.StatusId= 4 THEN 1                  when property.StatusId= 2 THEN 3                  when property.StatusId= 3 THEN 1                  when property.StatusId= 6 THEN 1                  when property.StatusId= 7 THEN 1                  when property.StatusId= 1 THEN 2                                 END ) FROM Property_New inner join Property on Property_New.PropertyID=Property.Propertyid where OrderId=1

Disable All constraints, Delete data in all tables and Enable all constraints Commands

Disable all constraints EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all" Delete data in all tables EXEC sp_MSForEachTable "DELETE FROM ?" Enable all constraints exec sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all" if some of the tables have identity columns we may want to reseed them EXEC sp_MSforeachtable "DBCC CHECKIDENT ( '?', RESEED, 0)"