Posts
Showing posts with the label sqlserver
Script to create SqlServer Table, Stored Procedure and View
- Get link
- X
- Other Apps
PRINT ' CREATE TABLE ' GO IF EXISTS ( SELECT * FROM dbo.sysobjects where id = object_id(N' [Table1] ') and OBJECTPROPERTY(id, N' IsUserTable ') = 1) DROP TABLE [ Table 1] GO CREATE TABLE [dbo].[ Table 1]( [Id] [ uniqueidentifier ] NOT NULL , [Name] [ varchar ](50) NULL , [DateUpdated] [ datetime ] NULL , CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED ( [Id] ) ) GO GRANT SELECT , INSERT , UPDATE , DELETE ON [ Table 1] TO [SampleUser] GO PRINT ' CREATE STORED PROCEDURE ' GO IF EXISTS ( SELECT * FROM dbo.sysobjects where id = object_id(N' [StoredProcedure1] ') and OBJECTPROPERTY(id, N' IsProcedure ') = 1) DROP PROCEDURE [dbo].[StoredProcedure1] GO CREATE PROCEDURE [dbo].[StoredProcedure1] AS SET NOCOUNT ON UPDATE dbo. Table 1 SET [DateUpdated]=GETDATE() GO GRANT EXECUTE ON [StoredProcedure1] TO [SampleUser] GO PRINT ' CREATE VIEW ' GO ...
Adding SQL Server CE 4.0 (Compact Edition) support to VS2010
- Get link
- X
- Other Apps
SQL Server GROUPING SETS
- Get link
- X
- Other Apps
DECLARE @Products TABLE ( Product varchar (50), Category varchar (50), Price float ) INSERT INTO @Products SELECT ' Sugar ',' Groceries ',2.35 UNION SELECT ' Bread ',' Groceries ',1.09 UNION SELECT ' Ecomonist ',' Magazines ',4.99 UNION SELECT ' FT ',' Magazines ',2.99 SELECT Product,Category, SUM (Price) AS Price, COUNT (*) AS [ Count ] FROM @Products GROUP BY GROUPING SETS((Product),(Category),()) ORDER BY Product DESC , Category DESC , Price DESC , [ Count ] DESC SELECT Product,[Range], SUM (Price) AS Price, AVG (Price) AS AvgPrice FROM ( SELECT Product, Price, [Range] = CASE WHEN Price BETWEEN 0 AND 1 THEN ' 1$ ' WHEN Price BETWEEN 1 AND 2 THEN ' 2$ ' WHEN Price BETWEEN 2 AND 3 THEN ' 3$ ' WHEN Price BETWEEN 3 AND 4 THEN ' 4$ ' WHEN Price BETWEEN 4 AND 5 THEN ' 5$ ' END FROM @Products ) a...
How to filter sp_helprotect information
- Get link
- X
- Other Apps
DECLARE @ sp_helprotect TABLE ( [Owner] VARCHAR (255), [Object] VARCHAR (255) NULL , [Grantee] VARCHAR (255) NULL , [Grantor] VARCHAR (255) NULL , [ProtectType] VARCHAR (255) NULL , [ Action ] VARCHAR (255) NULL , [ Column ] VARCHAR (255) NULL ) INSERT @ sp_helprotect EXEC sp_helprotect SELECT * FROM @ sp_helprotect WHERE [Object] = ' table-name ' ORDER BY [Object], [Grantee], [ Action ]
SQL Stored procedure with error handling using Try/Catch
- Get link
- X
- Other Apps
This is improved version of the script from 2009 using try/catch syntax USE [MyDatabase] GO PRINT ' DROP PROCEDURE [usp_mysproc] ' GO if exists ( select * from dbo.sysobjects where id = object_id(N' [dbo].[usp_mysproc] ') and OBJECTPROPERTY(id, N' IsProcedure ') = 1) drop procedure [dbo].[usp_mysproc] GO PRINT ' CREATE PROCEDURE [usp_mysproc] ' GO CREATE PROCEDURE [dbo].[usp_mysproc] ( @RaiseError bit = 0 ) AS BEGIN TRY BEGIN TRAN IF @RaiseError = 1 BEGIN DECLARE @ TEMP TABLE ( Column 1 varchar (2)) INSERT INTO @ TEMP ( Column 1) VALUES (' 123 ') END COMMIT TRAN END TRY BEGIN CATCH DECLARE @ErrorInfo VARCHAR (4000) SET @ErrorInfo = ' ERROR [ '+LTRIM(STR(ERROR_NUMBER()))+' ][ '+ERROR_PROCEDURE()+' : '+LTRIM(STR(ERROR_LINE()))+' ] - ' + ERROR_MESSAGE() PRINT CONVERT ( VARCHAR ,GETDATE(),21) + ' : '+@ErrorInfo ROLLBACK...
SQL Server How to find if any stored procedure is referencing database object
- Get link
- X
- Other Apps
SQL Server useful system stored procedures
- Get link
- X
- Other Apps
sp_helprotect [object_name] Returns a report with information about user permissions for an object, or statement permissions, in the current database. sp_helptext [view_name] Prints the text of a rule, a default, or an unencrypted stored procedure, user-defined function, trigger, computed column, or view. Full list of System Stored Procedures
How to filter sp_who2 information
- Get link
- X
- Other Apps
DECLARE @sp_who2 TABLE ( SPID INT , Status VARCHAR (255) NULL , Login SYSNAME NULL , HostName SYSNAME NULL , BlkBy SYSNAME NULL , DBName SYSNAME NULL , Command VARCHAR (255) NULL , CPUTime INT NULL , DiskIO INT NULL , LastBatch VARCHAR (255) NULL , ProgramName VARCHAR (255) NULL , SPID2 INT , REQUESTID INT ) INSERT @sp_who2 EXEC sp_who2 SELECT * FROM @sp_who2 WHERE Status > 'BACKGROUND'
Transaction management in SQL Server 2005
- Get link
- X
- Other Apps
This is an example of how to use try/catch functionality in SQL Server 2005 to manage transactions. USE [MyDatabase] PRINT 'Moving Data' GO BEGIN TRY BEGIN TRAN PRINT 'Inserting..' INSERT INTO table2(Id,ParentId,Name) SELECT t1.Id, t1.ParentId, t1.Name FROM Table1 t1 WHERE t1.ParentId = 1 PRINT 'Deleting..' DELETE FROM [Table1] i WHERE t1.ParentId = 1 COMMIT TRAN END TRY BEGIN CATCH ROLLBACK TRAN END CATCH GO
List view/table/storedprocedure details including creation, modify date
- Get link
- X
- Other Apps