Posts

Showing posts from October, 2012

Obtaining the database schema, tables, columns, and primary keys in a single SQL call

I am currently writing an EntityFramework reverse engineer code-first generator. It will generate POCO classes, DbContext and Code First mapping for an existing database. There is one already in Entity Framework Power Tools Beta 2 However this one is going to do the job right, and include table filtering! Watch this space... SELECT [Extent1].[SchemaName], [Extent1].[Name] AS TableName, [Extent1].[TABLE_TYPE] AS TableType, [UnionAll1].[Ordinal], [UnionAll1].[Name] AS ColumnName, [UnionAll1].[IsNullable], [UnionAll1].[TypeName], ISNULL([UnionAll1].[MaxLength],0) AS MaxLength, ISNULL([UnionAll1].[Precision], 0) AS Precision, ISNULL([UnionAll1].[Default], '') AS [Default], ISNULL([UnionAll1].[DateTimePrecision], '') AS [DateTimePrecision], ISNULL([UnionAll1].[Scale], 0) AS Scale, [UnionAll1].[IsIdentity], [UnionAll1].[IsStoreGenerated], CASE WHEN ([Project5...

FizzBuzz generators

SQL WITH mil AS ( SELECT TOP 1000000 ROW_NUMBER() OVER ( ORDER BY c.column_id ) [n] FROM master.sys.all_columns as c CROSS JOIN master.sys.all_columns as c2 ) SELECT CASE WHEN n % 3 = 0 THEN CASE WHEN n % 5 = 0 THEN 'FizzBuzz' ELSE 'Fizz' END WHEN n % 5 = 0 THEN 'Buzz' ELSE CAST(n AS char(6)) END + CHAR(13) FROM mil C# foreach (int number in Enumerable.Range(1, 100)) { bool isDivisibleBy3 = (number % 3) == 0; bool isDivisibleBy5 = (number % 5) == 0; if (isDivisibleBy3) Console.Write("Fizz"); if (isDivisibleBy5) Console.Write("Buzz"); if (!isDivisibleBy3 && !isDivisibleBy5) Console.Write(number); Console.WriteLine(); } C# linq Enumerable .Range(1, 100) .Select(i => i % 15 == 0 ? "FizzBuzz" : i % 5 == 0 ? "Buzz" : i % 3 == 0 ? "Fizz" : i.ToString()) .ToList() .For...

SQL Lotter number selector

Just a bit of fun with SQL WITH L0 AS (SELECT 0 AS C UNION ALL SELECT 0), L1 AS (SELECT 0 AS C FROM L0 AS A CROSS JOIN L0 AS B), L2 AS (SELECT 0 AS C FROM L1 AS A CROSS JOIN L1 AS B), L3 AS (SELECT 0 AS C FROM L2 AS A CROSS JOIN L2 AS B), Nums AS (SELECT TOP(49) ROW_NUMBER() OVER(ORDER BY (SELECT 0)) AS n FROM L3 ORDER BY n), Choice AS (SELECT TOP(6) n FROM Nums ORDER BY CHECKSUM(NEWID())) SELECT STUFF( (SELECT ',' + CAST(n AS VARCHAR(10)) AS [text()] FROM Choice ORDER BY n FOR XML PATH('')), 1, 1, '')