Last active
May 12, 2020 05:53
-
-
Save Turtlator/ae23ee49cec0e5c88977ff2d10bfd4a8 to your computer and use it in GitHub Desktop.
Create an SQL script for a table variable holding Enum values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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