Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

December 2007 - Posts

  • f_4P_columnTypeVB

    This is a helper function used in DDL scripts 

    -- =============================================
    -- v 1.5 
    -- =============================================
    IF EXISTS (SELECT * 
        FROM   sysobjects 
        WHERE  name = N'f_4P_columnTypeVB')
     DROP FUNCTION f_4P_columnTypeVB
    GO
    
    
    CREATE FUNCTION f_4P_columnTypeVB 
     (@intColumnType as int)
    
    
    -- select dbo.f_4P_columnTypeVB(56)
    
    
    RETURNS varchar(20)
    
    
    AS
    BEGIN
    declare @out varchar(20)
    
    
    select @out = 
     case when @intColumnType in(48,52, 56) then 'system.int64'
      when @intColumnType = 36 then 'GUID'
      when @intColumnType in(108,60,63,106) then 'double'
      when @intColumnType = 45 then 'boolean'
      when @intColumnType in (35,47,167, 175,231) then 'string'
      when @intColumnType = 61 then 'date'
      when @intColumnType = 104 then 'int16' --bit
      --when @intColumnType = 173 then 'binary(' + convert(varchar(4),@intLength) + ')'
      else 'dunno - ' + convert(varchar(4),@intColumnType)
      end 
    
    
     return @out
    
    
    
    END
    GO
    
    
     
    
    
     

  • Script to create a SELECT statement

    The following script will create a SELECT statement for the table supplied as a parameter.

     You'll need these two scripts also

    f_4P_columnType

    f_4P_columnTypeVB

    -- =============================================
    -- 
    -- =============================================
    -- 1/1/1900 created
    IF EXISTS (SELECT name 
        FROM   sysobjects 
        WHERE  name = N'sp_select' 
        AND    type = 'P')
        DROP PROCEDURE sp_select
    GO
    
    
    CREATE PROCEDURE sp_select 
    -- sp_select 'groups'
    
    
    @vchrTableName varchar(50)
    
    
    
    AS
    
    
    set nocount on
     
    declare @vchrProcName varchar(100)
    declare @vchrFieldName varchar(100)
    declare @vchrFieldType varchar(100)
    declare @vchrFieldTypeVB varchar(100)
    declare @vchrParamName varchar(100)
    declare @intColstat int
    declare @vchrParamList varchar(1000)
    declare @vchrSelectList varchar(1000)
    
    
    select
     @vchrParamList = '',
     @vchrSelectList = '',
    -- @vchrParamListVB = '',
    -- @vchrParamListHelper = '',
    -- @output = '',
     @vchrProcName = '_4P_' + upper(@vchrTableName) + '_SEL',
     @vchrTableName = upper(@vchrTableName)
    
    
    
    print 'IF EXISTS (SELECT name '
    print '    FROM   sysobjects '
    print '    WHERE  name = N' + char(39) + @vchrTableName + char(39)
    print '        AND type = ' + char(39) + 'P' + char(39) + ')'
    print '    DROP PROCEDURE ' + @vchrProcName
    print 'GO'
    PRINT ''
    print 'CREATE PROCEDURE ' + @vchrProcName
    print ''
    
    
    declare curFields CURSOR for
    select syscolumns.name,
      dbo.f_4P_columnType (xusertype,length, xprec , xscale),
      dbo.f_4P_columnTypeVB (xusertype),
      <a href="mailto:'@'">'@'</a> + syscolumns.name,
      syscolumns.colstat
     from sysobjects
      left join syscolumns on syscolumns.id = sysobjects.id
     where sysobjects.name = @vchrTableName
    
    
    -- sp_select 'groups'
    
    
    DECLARE @vchrWhere varchar(1000)
    set @vchrWhere = ''
    
    
    OPEN curFields
    FETCH NEXT FROM curFields INTO @vchrfieldName, @vchrFieldType, @vchrFieldTypeVB,@vchrparamName, @intColstat
    WHILE @@FETCH_STATUS = 0
    BEGIN
     --build a where clause
     if @intColStat = 1 begin
      set @vchrWhere = @vchrWhere  + '        ' + @vchrFieldName + ' = ' + @vchrparamName+ char(13)
      
      --build a param list (builds @name varchar(20) )
      set @vchrParamList = @vchrParamList + char(13) + @vchrparamName + ' ' + @vchrFieldType + ','
     end
    
    
     --build the 'set' list
     set @vchrSelectList = @vchrSelectList + char(13)  + space(8) + @vchrFieldName + ','
    
    
     FETCH NEXT FROM curFields INTO @vchrfieldName, @vchrFieldType,@vchrFieldTypeVB, @vchrparamName, @intColstat
    
    
    
    END
    CLOSE curFields
    DEALLOCATE curFields
    
    
    --remove the last char from the list
    set @vchrParamList = left(@vchrParamList, len(@vchrParamList)-1)
    
    
    --remove the first and last char from the list
    set @vchrSelectList = stuff(@vchrSelectList,1,1,'')
    set @vchrSelectList = left(@vchrSelectList, len(@vchrSelectList)-1)
    
    
    print @vchrParamList
    print ''
    PRINT 'AS'
    print ''
    Print 'SELECT'
    print @vchrSelectList
    print '    FROM ' + @vchrTableName + ' WITH (NOLOCK)'
    
    
    print '    WHERE'
    print @vchrWhere
    PRINT 'GO'
    PRINT 'GRANT EXEC ON ' + @vchrProcName + ' TO PUBLIC'
    -- sp_select 'groups'
    
    
    go
    
    
    grant exec on sp_select to public
    
    
    
     
    
    
     

  • What is the best recovery model for SQL Server?

    From a recent email: 

    What, in your opinion, is the best recovery model for a GP database?  Full? Bulk_Logged? Simple?  They all seem to have their pros & cons but I can't find anyplace where someone says this model is better than the other two because of X, Y or Z reasons.
     

    A:

    Simple
    If you use Simple, transaction logging is disabled. Simply backing up the database will truncate the transaction logs. (If the logs are already too big, run this script http://vstoolsforum.com/blogs/sqlserver/archive/2007/01/11/compact-sql-logs.aspx). So, if the client is OK with their restore point being last night, when the backups ran, then Simple is fine. It's easier to maintain.

    Bulk_Logged
    I've never used Bulk_Logged, so I can't speak to that.

    Full
    Usually the client will want to be able to restore a crashed server up to the last hour, so we use Full. Using Full, the act of running a transaction log backup is what truncates the log. So, you're running a backup every evening and running transaction log backups every hour.

    Some of the system databases don't support Full, so I usually have two Maintenance Plans - one for the system dbs and one for the user dbs. I never use the 'these specific databases' option because the next time that a database is added to the server, you'll forget to add it to the backup. Choosing the 'all user dababases' option stops that worry.


More Posts Next page »