Skip to content

Instantly share code, notes, and snippets.

@Turtlator
Last active May 12, 2020 05:53
Show Gist options
  • Select an option

  • Save Turtlator/ae23ee49cec0e5c88977ff2d10bfd4a8 to your computer and use it in GitHub Desktop.

Select an option

Save Turtlator/ae23ee49cec0e5c88977ff2d10bfd4a8 to your computer and use it in GitHub Desktop.
Create an SQL script for a table variable holding Enum values
public string EnumTable<T>() where T: Enum
{
var DeclareTable = $"DECLARE @{typeof(T).Name} TABLE ( Id INT, Value NVARCHAR(50) )\n";
var insert = $"INSERT INTO @{typeof(T).Name} (Id, Value)\nVALUES";
var values = Enum.GetValues(typeof(T))
.Cast<T>()
.Select(v => $"({Convert.ToInt32(v)}, '{v.ToString()}')");
return DeclareTable + insert + string.Join(",\r\n", values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment