Mostrando entradas con la etiqueta Plantillas SQL Server. Mostrar todas las entradas
Mostrando entradas con la etiqueta Plantillas SQL Server. Mostrar todas las entradas

Plantilla para crear nuevos Indices en SQL Server



-- Add non unique index for a single field
IF NOT EXISTS (SELECT * FROM sysindexes WHERE name = 'inu_TTTTT_FFFFFF')
   CREATE INDEX inu_TTTTT_FFFFFF ON TTTTT(FFFFFF)

--------------------------------------------------------------------------------------------------

-- Add clustered non unique index for a single field
IF NOT EXISTS (SELECT * FROM sysindexes WHERE name = 'inu_TTTTT_FFFFFF')
   CREATE CLUSTERED INDEX inu_TTTTT_FFFFFF ON TTTTT(FFFFFF)

--------------------------------------------------------------------------------------------------

-- Add unique index for a single field
IF NOT EXISTS (SELECT * FROM sysindexes WHERE name = 'iun_TTTTT_FFFFFF')
   CREATE UNIQUE INDEX iun_TTTTT_FFFFFF ON TTTTT(FFFFFF)

--------------------------------------------------------------------------------------------------

-- Add unique index for a combination of two fields
IF NOT EXISTS (SELECT * FROM sysindexes WHERE name = 'iun_TTTTT_FFFFFF_GGGGGG')
   CREATE UNIQUE INDEX iun_TTTTT_FFFFFF_GGGGGG ON TTTTT(FFFFFF, GGGGGG)

--------------------------------------------------------------------------------------------------

Plantilla para crear una Vista en SQL Server



IF EXISTS (SELECT *
           FROM INFORMATION_SCHEMA.VIEWS
           WHERE TABLE_NAME='vXXXXX'
           AND TABLE_SCHEMA='dbo'
          )
          DROP VIEW dbo.vXXXXX
GO

CREATE VIEW dbo.vXXXXX
AS
/*@@*_____________________________________________________________________

  (c) 2013 XXXXX

  Object:  dbo.vXXXXX
  Author:  ...
  Created: ...

  Purpose:

    ...
    ...
    ...


  Revision History:
    Author dd-mmm-yyyy Purpose

_____________________________________________________________________*@@*/


  SELECT
    ...
  FROM
    ...


Plantilla para crear un nuevo Procedimiento Almacenado en SQL Server



IF EXISTS (SELECT *
           FROM INFORMATION_SCHEMA.ROUTINES
           WHERE ROUTINE_NAME='pXXXXX'
           AND ROUTINE_SCHEMA='dbo'
           AND ROUTINE_TYPE='PROCEDURE'
          )
          DROP PROCEDURE dbo.pXXXXX
GO

CREATE PROC dbo.pXXXXX
  @vintCurUserID        INT,
  @vintCurSysLID        INT
AS
/*@@*_____________________________________________________________________

  (c) 2013 XXXXX

  Object:  pXXXXX
  Author:  ...
  Created: ...

  Purpose:

    ...
    ...
    ...


  Parameters:
    @vintCurUserID:  Current user context if available. NULL if unknown.
    @vintCurSysLID:  Language for translated text. NULL if unknown.

  Recordsets:

  Returns:
    0         - Success
    <>0       - Error occurred

  Revision History:
    Author dd-mmm-yyyy Purpose

_____________________________________________________________________*@@*/


/*______________________ CONSTANT DECLARATIONS _________________________*/

  DECLARE
    @cstrThisProc              NVARCHAR(128),
    @cintErrGeneral            INT

  SET @cstrThisProc            = N'pXXXXX'
  SET @cintErrGeneral          = 50000  -- General error. Used when an
                                        -- error has occurred and no other
                                        -- specific error condition is
                                        -- assigned.

/*______________________ VARIABLE DECLARATIONS _________________________*/

  DECLARE
    @intErrNo       INT,
    @strErrMsg      NVARCHAR(400),
    @intReturn      INT

/*_______________________________ INIT _________________________________*/

  -- Setting NOCOUNT ON prevents additional recordsets with the
  -- "number of records affected" from being returned, which is not
  -- handled the same by all providers, such as for OLEDB vs. ODBC.
  SET NOCOUNT ON

/*________________________ VALIDATE PARAMETERS _________________________*/


/*_______________________________ MAIN _________________________________*/



  -- Return value 0 indicates success
  SET @intReturn = 0

lblExit:

  RETURN @intReturn

/*___________________________ ERROR HANDLER ____________________________*/
lblError:
  -- An error has occurred.  On entering this section @intErrNo should
  -- already hold the error number

  -- By default, if there are any open transactions then roll them back
  IF (@@TRANCOUNT > 0) BEGIN
    ROLLBACK TRAN

  END

  -- If return value has not been explicitly set, then use the error
  -- number as return value to signify failure
  IF ((@intReturn IS NULL) OR (@intReturn = 0)) BEGIN
    SET @intReturn = @intErrNo

  END

 GOTO lblExit

Plantilla para crear nuevos campos en SQL Server



-- Add new int field
IF NOT EXISTS(SELECT [Name] FROM syscolumns WHERE Name = 'FFFFFF' AND ID = OBJECT_ID('dbo.TTTTT'))
   ALTER TABLE TTTTT ADD FFFFFF INT NULL

--------------------------------------------------------------------------------------------------

-- Add new nvarchar field
IF NOT EXISTS(SELECT [Name] FROM syscolumns WHERE Name = 'FFFFFF' AND ID = OBJECT_ID('dbo.TTTTT'))
   ALTER TABLE TTTTT ADD FFFFFF NVARCHAR(60) NULL

--------------------------------------------------------------------------------------------------

-- Add new decimal field
IF NOT EXISTS(SELECT [Name] FROM syscolumns WHERE Name = 'FFFFFF' AND ID = OBJECT_ID('dbo.TTTTT'))
   ALTER TABLE TTTTT ADD FFFFFF DECIMAL(28,9) NULL

--------------------------------------------------------------------------------------------------

-- Add new bit field (incl. update existing records with 0)
IF NOT EXISTS(SELECT [Name] FROM syscolumns WHERE Name = 'FFFFFF' AND ID = OBJECT_ID('dbo.TTTTT')) BEGIN
   ALTER TABLE TTTTT ADD FFFFFF BIT CONSTRAINT [rdf_TTTTT_FFFFFF] DEFAULT (0)
   EXEC ('UPDATE TTTTT SET FFFFFF = 0')
END

--------------------------------------------------------------------------------------------------

-- Add new ntext field
IF NOT EXISTS(SELECT [Name] FROM syscolumns WHERE Name = 'FFFFFF' AND ID = OBJECT_ID('dbo.TTTTT'))
   ALTER TABLE TTTTT ADD FFFFFF NTEXT NULL

--------------------------------------------------------------------------------------------------

Crear plantillas personalizada en SQL Server Management Studio

SQL Server tiene una variedad de plantillas que te ayudarán a crear objetos en tu base de datos. Si aún no ves el formulario de plantillas, puedes hacerlo visible en el menú "Ver" -> "Explorador de plantillas".

Las plantillas predeterminadas por el SQL Server no están mal pero muchas veces las empresas tienen sus propias normas, por ejemplo para la documentación del código. La utilización de plantillas en fundamental cuando se desarrolla en equipo y se quiere estandarizar los formatos. Esto garantiza que todos los desarrolladores sigan unas normas y así se consigue que se cumplan más fácilmente las políticas de desarrollo de la empresa.

Aparte de las plantillas que te ofrece el SQL Server puedes también crear tus propias plantillas personalizadas. Pincha sobre el primer nodo "Plantilla de SQL Server" y crea una nueva carpeta o directamente la plantilla.