So I've been tasked with creating a tool to allow ours users to create their own 'worklists' which they use to work through their data. In our app these worklists are driven by SQL views, so for now my program is having to dynamically create views in our database based on the users input. I don't like this, but for now I have to make the best of it an am brainstorming the best ways to go about this.
Basically every view I create has a similar skeleton, it has several columns that are always pulled and several joins that always happens. Based on the users input I may add additional SELECT columns, as well as additional joins if they are necessary to access the added display columns.
So basically right now my code looks like this...
string SQL = string.Format(@"CREATE VIEW {0}
AS
SELECT
Foo.A,
Bar.B,
{1}
FROM
Table
INNER JOIN Foo on Foo.ID = Table.FooID
INNER JOIN Bar on Bar.ID = Table.BarID
{2}", viewName, displayNames, extraJoins);
Database.ExecuteNonQuery(SQL);
I really don't like this for obvious reasons. However, I cannot seem to find the equivalent of a parametrized query for view creation with ADO. I could perhaps create a stored procedure to do this, but even that seems sloppy. Is there any reasonable way to do something like this that doesn't make me sick to my stomach? Also we are using MS SQL, and have to support as far back as 2005.