Code samples in the forums

  • I've been adapting the Simple-Talk prettifier to produce IFCodes, so you can paste code straight into forum entries. The idea is a bit clunky when there would normally be a or as in the wikis, but it would be better than plain dark-blue text.

    so, instead of...(pasted in directly from SSMS)

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

    ALTER FUNCTION [dbo].[ufbTrueOrFalse]

    (@TrueOrFalse sql_Variant

    )

    /* routine for correcting the input from the presentation

    layer to give a correct 'true' or 'false' however it is

    represented in the parameter passed */

    RETURNS BIT--returns 1 if true or 0 if false

    AS

    begin

    return

    (case @TrueOrFalse

    when -1 then 1

    when 1 then 1

    when 0 then 0

    when 'true' then 1

    when 'false' then 0

    when 'yes' then 1

    when 'no' then 0

    when 'on' then 1

    when 'off' then 0

    else 0

    end)

    end

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

    ...you get....

    [font="Courier New"]

    ALTER FUNCTION [dbo].[ufbTrueOrFalse]

                  (@TrueOrFalse sql_Variant

                   )

    /* routine for correcting the input from the presentation

    layer to give a correct 'true' or 'false' however it is

    represented in the parameter passed */               

    RETURNS BIT--returns 1 if true or 0 if false

    AS

    BEGIN

    RETURN

       (CASE @TrueOrFalse 

            WHEN -1 THEN 1

            WHEN 1 THEN 1

            WHEN 0 THEN 0

            WHEN 'true' THEN 1

            WHEN 'false' THEN 0

            WHEN 'yes' THEN 1

            WHEN 'no' THEN 0

            WHEN 'on' THEN 1

            WHEN 'off' THEN 0

          ELSE 0

       END)

    END

    [/font]

    ...hmmm, not bad, but size 1 is too small to read in my browser and size 2 (which I'm using) might just be considered a bit big in comparison to the body text.

    ...you should be able to put it in a code block but it comes out with lots of line-breaks. It would be great to have a fix for this as I'd like to release the updated prettifier...

    And, heaven only knows how much I hate the silver background to the code block. It makes the colours difficult to read. If you want a background colour make it subtle please!

    [font="Courier New"]

    ALTER FUNCTION [dbo].[ufbTrueOrFalse]

                  (@TrueOrFalse sql_Variant

                   )

    /* routine for correcting the input from the presentation

    layer to give a correct 'true' or 'false' however it is

    represented in the parameter passed */               

    RETURNS BIT--returns 1 if true or 0 if false

    AS

    BEGIN

    RETURN

       (CASE @TrueOrFalse 

            WHEN -1 THEN 1

            WHEN 1 THEN 1

            WHEN 0 THEN 0

            WHEN 'true' THEN 1

            WHEN 'false' THEN 0

            WHEN 'yes' THEN 1

            WHEN 'no' THEN 0

            WHEN 'on' THEN 1

            WHEN 'off' THEN 0

          ELSE 0

       END)

    END

    [/font]

    Best wishes,
    Phil Factor

  • In IE 6, the spaces appear as squares, making the code difficult to read.  I agree that the double (or is it triple?) spacing is very annoying.

    Tony.

  • I can't edit the post I did earlier (it puts in extra line-feeds wherever I've pasted anything in) but if you are using IE6 or IE6, the code will look awful. This is because EE6 doesn't understand the HTML 'N-space' character. This means that Steve and Steve will have to take out the routine that removes non-breaking spaces in the normal forum text.

    The code above looks fine in Safari, Firefox and IE7. Opera takes out the   characters altogether but otherwise renders it OK. Shame, but there are just too many IE6 users around still. (maybe they don't like IE7's occasional tendency for sudden death)

    Best wishes,
    Phil Factor

  • Very nice, Phil!!!!

    We'll see if we can get this added and we're working on the double/triple spacing.

  • We`re making progress now with the struggle to produce nicely-formatted SQL. We are a few tweaks short of a SQL Server forum still (Having a SQL Server forum that can`t display SQL code is like having Youtube that can't show video)

    Current issues are:

    1/ if you edit a post or preview it, the spaces in code blocks disappear and the code loses its indentation,

    2/ The code block has a grey background. As grey is one of the highlighting colours used in the Query Analyser, this makes code difficult to read.

    3/ indentation in inline code is best done with non-breaking spaces because ordinary spaces aren't displayed (they seem to be stripped out), but non-break spaces are stripped out by the forum Software. the N-spaces and M-Spaces aren't stripped out but aren't recognised by IE6!

    4/ The font sizes 1..7 aren't particularly useful. Font size 2 is a bit big, and font size 1 is a bit too small. I can't quite see why anyone would use 4..7

    Best wishes,
    Phil Factor

  • Phil,
    1/ I pasted the code below from the HTML tabe of the Pretiffier into the HTML tab of the 
    editor (not available to non-admins) and edited it 3 times
    USE [master]
    GO
    CREATE DATABASE [Test_DB] ON  PRIMARY
       (
                NAME N'Test_DB_2',
                FILENAME N'D:\MSSQL\Data\Test_DB.mdf' ,
                SIZE 102400KB ,
                MAXSIZE UNLIMITED,
                FILEGROWTH 102400KB
       )
             LOG ON
       (
                NAME N'Test_DB_log',
                FILENAME N'E:\MSSQL\Logs\Test_DB_log.ldf' ,
                SIZE 51200KB ,
                MAXSIZE 2048GB ,
                FILEGROWTH 51200KB
       )
             COLLATE SQL_Latin1_General_CP1_CI_AS
    GO
             

    2. Agreed.

    3. I'm not sure I follow on this one. The code above uses non-breaking spaces and they don't seem to be stripped out.

    4. It would be useful to have a font size between 1 and 2.

    Cheers,

    Tony.

  • Pasted in from rendered code...

    CREATE FUNCTION capitalize ( @String VARCHAR(8000) )

    /*Returns a copy of s with its first character capitalized,

    and the rest of the characters lowercased.

    e.g.

    Select dbo.capitalize('this works properly')

    Select dbo.capitalize('

    this works properly')

    Select dbo.capitalize(' this works properly')

    Select dbo.capitalize('3dthis works properly')

    Select dbo.capitalize('')

    Select dbo.capitalize(null)

    */

    RETURNS VARCHAR(8000)

    AS BEGIN

    -- Declare the return variable here

    DECLARE @Result VARCHAR(8000)

    DECLARE @FirstAsciiChar INT

    SELECT @FirstASCIIChar = PATINDEX('%[^a-z][a-z]%',

    ' ' + COALESCE(@String, ''))

    IF @FirstASCIIChar > 0

    SELECT @Result = STUFF(@string, @FirstASCIIChar, 1,

    UPPER(SUBSTRING(@string, @FirstASCIIChar, 1)))

    ELSE

    SELECT @Result = @String

    RETURN @Result

    END

    GO

    when I pasted HTML code into a code block it was too horrible to look at. I've tried every combination and can't get the results you have.

    I wonder if this is a browser difference

    Best wishes,
    Phil Factor

  • This is pasted from the Rendered HTML tab of the prettifier into the Design window. When I initally paste it in to the editor, it adds in extra carriage returns between each line, which I had to manually remove. This is obviously a problem, but once done, it seems to maintain formatting correctly (note: I'm working in IE6):

    USE [master]

    GO

    CREATE DATABASE [Test_DB] ON  PRIMARY

      (

            NAME N'Test_DB',

            FILENAME N'D:\MSSQL\Data\Test_DB.mdf' ,

            SIZE 102400KB ,

            MAXSIZE UNLIMITED,

            FILEGROWTH 102400KB

      )

          LOG ON

      (

            NAME N'Test_DB_log',

            FILENAME N'E:\MSSQL\Logs\Test_DB_log.ldf' ,

            SIZE 51200KB ,

            MAXSIZE 2048GB ,

            FILEGROWTH 51200KB

      )

          COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

  • ....and this is what happens when copy and paste from the 'rendered' tab in the prettifier in IE6. I'm baffled as to why the difference. I get the same in IE7 on XP too

    ALTER FUNCTION dbo.[count]

    (

    @string VARCHAR(8000),

    @Sub VARCHAR(8000),

    @start INT = NULL,

    @end INT = NULL

    )

    /* Returns the number of occurrences of substring sub in string s.

    Select dbo.count('This is a nice string','[^a-z][a-z]',null,null)--wordcount (not include first word)

    Select dbo.count('I''m henery the eighth I am I am','I am',null,null)

    select dbo.count('45667892398','8',null,null)

    */

    RETURNS INT

    AS BEGIN

    DECLARE @more INT

    DECLARE @count INT

    IF @string = NULL

    RETURN NULL

    SELECT @count = 0, @more = 1, @Start = COALESCE(@Start, 1),

    @end = COALESCE(@end, LEN(@string))

    SELECT @end = CASE WHEN @end > LEN(@string) THEN LEN(@string)

    ELSE @end

    END

    WHILE @more <> 0

    BEGIN

    SELECT @more = PATINDEX('%' + @sub + '%',

    SUBSTRING(@string, @Start, @End - @start + 1))

    IF @more > 0

    SELECT @Start = @Start + @more, @count = @count + 1

    IF @start >= @End

    SELECT @more = 0

    END

    RETURN @count

    END

    GO

    Best wishes,
    Phil Factor

  • Working as genral Forum Member rather than Admin.

    Rendered code pasted from prettifier

    USE [master]

    GO

    CREATE DATABASE [Test_DB] ON PRIMARY

    (

    NAME = N'Test_DB',

    FILENAME = N'D:\MSSQL\Data\Test_DB.mdf' ,

    SIZE = 102400KB ,

    MAXSIZE = UNLIMITED,

    FILEGROWTH = 102400KB

    )

    LOG ON

    (

    NAME = N'Test_DB_log',

    FILENAME = N'E:\MSSQL\Logs\Test_DB_log.ldf' ,

    SIZE = 51200KB ,

    MAXSIZE = 2048GB ,

    FILEGROWTH = 51200KB

    )

    COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

  • This is pasted in from rendered window within code blocks

    USE [master]

    GO

    CREATE DATABASE [Test_DB_2] ON PRIMARY

    (

    NAME = N'Test_DB',

    FILENAME = N'D:\MSSQL\Data\Test_DB.mdf' ,

    SIZE = 102400KB ,

    MAXSIZE = UNLIMITED,

    FILEGROWTH = 102400KB

    )

    LOG ON

    (

    NAME = N'Test_DB_log',

    FILENAME = N'E:\MSSQL\Logs\Test_DB_log_2.ldf' ,

    SIZE = 51200KB ,

    MAXSIZE = 2048GB ,

    FILEGROWTH = 51200KB

    )

    COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

  • Pasted from SSMS into design window using admin interface:

    USE [master]

    GO

    CREATE DATABASE [Test_DB] ON  PRIMARY

      (

            NAME = N'Test_DB',

            FILENAME = N'D:\MSSQL\Data\Test_DB.mdf' ,

            SIZE = 102400KB ,

            MAXSIZE = UNLIMITED,

            FILEGROWTH = 102400KB

      )

          LOG ON

      (

            NAME = N'Test_DB_log',

            FILENAME = N'E:\MSSQL\Logs\Test_DB_log.ldf' ,

            SIZE = 51200KB ,

            MAXSIZE = 2048GB ,

            FILEGROWTH = 51200KB

      )

          COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

  • trying again as general forum member now that tab compression is off:

    USE [master]

    GO

    CREATE DATABASE [Test_DB] ON PRIMARY

    (

    NAME = N'Test_DB',

    FILENAME = N'D:\MSSQL\Data\Test_DB.mdf' ,

    SIZE = 102400KB ,

    MAXSIZE = UNLIMITED,

    FILEGROWTH = 102400KB

    )

    LOG ON

    (

    NAME = N'Test_DB_log',

    FILENAME = N'E:\MSSQL\Logs\Test_DB_log.ldf' ,

    SIZE = 51200KB ,

    MAXSIZE = 2048GB ,

    FILEGROWTH = 51200KB

    )

    COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

  • I've got no admin permissions, but here's a code block:

    USE [master]

    GO

    CREATE DATABASE [Test_DB] ON PRIMARY

    (

    NAME = N'Test_DB',

    FILENAME = N'D:\MSSQL\Data\Test_DB.mdf' ,

    SIZE = 102400KB ,

    MAXSIZE = UNLIMITED,

    FILEGROWTH = 102400KB

    )

    LOG ON

    (

    NAME = N'Test_DB_log',

    FILENAME = N'E:\MSSQL\Logs\Test_DB_log.ldf' ,

    SIZE = 51200KB ,

    MAXSIZE = 2048GB ,

    FILEGROWTH = 51200KB

    )

    COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

    I have edited this too.

  • this uses the new IFCODE prettifier and code tags and is pasted from the rendered html tab

    USE [master]

    GO

    CREATE DATABASE [Test_DB] ON PRIMARY

    (

    NAME = N'Test_DB',

    FILENAME = N'D:\MSSQL\Data\Test_DB.mdf' ,

    SIZE = 102400KB ,

    MAXSIZE = UNLIMITED,

    FILEGROWTH = 102400KB

    )

    LOG ON

    (

    NAME = N'Test_DB_log',

    FILENAME = N'E:\MSSQL\Logs\Test_DB_log.ldf' ,

    SIZE = 51200KB ,

    MAXSIZE = 2048GB ,

    FILEGROWTH = 51200KB

    )

    COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

    Color is not preserved. Below is the same but pasted from the Source HTML tab:

    [font="Courier New"]USE [master]

    GO

    CREATE DATABASE [Test_DB] ON PRIMARY

    (

    NAME = N'Test_DB',

    FILENAME = N'D:\MSSQL\Data\Test_DB.mdf' ,

    SIZE = 102400KB ,

    MAXSIZE = UNLIMITED,

    FILEGROWTH = 102400KB

    )

    LOG ON

    (

    NAME = N'Test_DB_log',

    FILENAME = N'E:\MSSQL\Logs\Test_DB_log.ldf' ,

    SIZE = 51200KB ,

    MAXSIZE = 2048GB ,

    FILEGROWTH = 51200KB

    )

    COLLATE SQL_Latin1_General_CP1_CI_AS

    GO

    [/font]

    Success!

Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic. Login to reply