McKelt.com

Remembering Thoughts

 

Recent comments

Authors

Categories


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

SQL Tricks

 Grant execute permissions to all stored procedures to a specific user


CREATE PROC grantexecutepermission(
           @UserName NVARCHAR(250))
AS
  DECLARE curse CURSOR  FOR
  SELECT name
  FROM   sysobjects
  WHERE  TYPE = 'P'
                
  OPEN curse
  
  DECLARE  @proc VARCHAR(100)
  
  DECLARE  @stmt NVARCHAR(200)
                 
  FETCH NEXT FROM curse
  INTO @proc
  
  WHILE @@FETCH_STATUS = 0
    BEGIN
      SET @stmt = 'grant execute on ' + @proc + ' to ' + @UserName
      
      EXEC sp_executesql
        @STMT
      
      PRINT @stmt
      
      FETCH NEXT FROM curse
      INTO @proc
    END
    
  CLOSE curse
  
  DEALLOCATE curse

 

Check if column exists before adding

 

IF NOT EXISTS (select * from Information_SCHEMA.columns
WHERE Table_name='ExampleTable' and column_name='ExampleColumn')
BEGIN
ALTER TABLE ExampleTable
ADD ExampleColumn nvarchar(350)
END
GO
 

Use a cursor to print out column values to a pre-formatted string

SET NOCOUNT ON

DECLARE @BTDocumentId int
DECLARE @BTDocumentVersionNo int
DECLARE myCursor CURsOR FOR
SELECT Id as BTDocumentId, VersionNo as BTDocumentVersionNo from document where istemplate = 1
OPEN myCursor
FETCH NEXT FROM myCursor INTO @BTDocumentId, @BTdocumentVersionNo
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'BTVersionId=' +  CAST(@BTDocumentId AS VARCHAR(500)) + ' AND BTDocumentVersionNo=' + CAST(@BTDocumentVersionNo AS VARCHAR(500)) + ', '
FETCH NEXT FROM myCursor INTO @BTDocumentId, @BTdocumentVersionNo
END 
CLOSE myCursor
DEALLOCATE myCursor  

Reseed a table

 

DBCC CHECKIDENT ("Risk", RESEED, 920617);

Posted by chris on Thursday, July 31, 2008 9:30 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Comments

Larry The Cable Guy: Tailgate Party Trailer United States

Thursday, June 24, 2010 2:41 AM

Larry The Cable Guy: Tailgate Party Trailer

Amazing Post.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading