SQL Sudoko

  • Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/dpoole/sqlsudoko.asp

  • This is not the first TSQL Sudoku puzzle solver/generator (see, for example The Sudoku Puzzle Generator. However, it is nicely explained. The two approaches are completely different. Perhaps, with Phil's puzzle generator and this solver, a SQL Server system could keep itself amused for ever!

  • Other sql soduko's:


    Nice job David P. ūüôā


    You must unlearn what You have learnt

  • I'm not a fan of Sudoko, but I really enjoyed this article.  Good work!

    John Scarborough

  • I like Sudokus, the hard one's particularly. Couple of observations :

    1) Very nice and all, but perhaps you have a little too much time on your hands?

    2) Slippery little algorithms for Sudokus aren't they? I'd never made the relationship between them and SQL server, I'm thinking they're good brain food for coding....

    All in all, impressive, but pointless

    Keep up the good work...


  • I think this is great and plan on spending the next two weeks testing !  Who cares if all the other applications I support go down ... I'll be busy testing

  • I think I am missing something.  I have done all the code but it is not working right for me.

  • Two things are missing as far as I can see.

    There is no CREATE-statement for the Sudoko table. But since the schema for the table is shown, it is trivial to put this in:

    IF EXISTS (SELECT * FROM sys.tables WHERE Name='Sudoko')

    DROP TABLE Sudoko

    CREATE TABLE Sudoko (

    RowId TinyINT NOT NULL,

    ColumnID TinyINT NOT NULL,

    CellValue TinyINT NULL,

    CONSTRAINT pk_sudoko PRIMARY KEY (RowID, ColumnID)


    A more serious problem occurs at the end of the final implementation of SolveSingleCells - where the sp calls the procedure RemoveSolvedCells. This procedure is not defined anywhere in the article.


    Jens Gyldenkærne Clausen

  • Nice job.  Now I don't feel so silly for writing a FORTH interpreter in T-SQL.





  • Phil Factor's solution that I mentioned earlier in the thread uses the same approach as the one Joe Celco mentions that he uses. It is, as he says, far neater. The Wikipedia entry on Sudoku is excellent. It does touch on the various computer approaches to constructing and solving the puzzle Sudoku in the Wikipedia

  • I noticed the same things too.

    Creating the table sudoko was pretty trivial.

    Of greater concern is the missing RemoveSolvedCells SP.

    Also, since I am using MS SQL Server 2000, I had to change all the "sys.views," "sys.tables," "sys.procedures," etc. to sysobjects.

    Some way to print out the unsolved and solved puzzle in a user readable format would be a nice addition.  Something that looks like this:





























  • I think I may have found a puzzle that this one doesn't solve. (Though it could just be that my implementation is wrong.)














    I found this puzzle at http://www.websudoko.com

  • Can I get someone that has gotten this to work upload their code or something so that I can verify it with my code?  My code is not working correctly.



  • My apologies for the missing stored procedure.

    When I started building my solution I prototyped it by bouncing values through tables and the procedure was used to remove solved cells from a working table.

    Of course once I worked out how to do the entire thing using views the procedure became redundant.

    There is a form of Sudoko that I have seen called Ariadne's thread.

    Ariadne is the person who supplied a Greek hero (Perseus?) with a thread so he could find his way around the Minotaur's labyrynth.

    With this form of Sudoko you will reach a point where a decision has to be made between 2 values in a particular cell. Choose the wrong value and the puzzle won't solve, therefore you have to retrace your steps until you can fill in the alternative value.

    Obviously, this T-SQL solution will not cater for this type of puzzle.

  • First off, cool article

    I'm also using SQL 2000, so instead of using sys.views I use:

    IF object_id('Digits') is not null drop view Digits

    Here is a way to print the Sudoko table using SQL 2000. You need to change the options in query analyzer to not print column headers and set the output format to "space delimited".

    select '*','-','-','-','-','-','-','-','-','-','-','-','*'

    union all

    select --rowId


    ,max(case columnId when 1 then isnull(cast(cellValue as char(1)),'.') end)

    ,max(case columnId when 2 then isnull(cast(cellValue as char(1)),'.') end)

    ,max(case columnId when 3 then isnull(cast(cellValue as char(1)),'.') end)


    ,max(case columnId when 4 then isnull(cast(cellValue as char(1)),'.') end)

    ,max(case columnId when 5 then isnull(cast(cellValue as char(1)),'.') end)

    ,max(case columnId when 6 then isnull(cast(cellValue as char(1)),'.') end)


    ,max(case columnId when 7 then isnull(cast(cellValue as char(1)),'.') end)

    ,max(case columnId when 8 then isnull(cast(cellValue as char(1)),'.') end)

    ,max(case columnId when 9 then isnull(cast(cellValue as char(1)),'.') end)


    from sudoko

    where rowId 6

    group by RowId

    union all

    select '*','-','-','-','-','-','-','-','-','-','-','-','*'

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

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