'Fully dynamic Stored SQL Procedures?

Is it possible to have fully dynamic SQL Stored procedures? I've read up on dynamic SQL for the past few hours but couldn't find the "fully dynamic" solution that I am looking for.

I want to make a procedure that allows for imports of all files of that type, just by simply passing the file location, table name, the rows data and value types. It shouldn't care how many columns there has to be, what datatypes are used etc.

Reason for this is that we are importing a lot of different types of .csv files, and having to make a new stored procedure and table for it sounds way more time consuming than just having to make the table and use a common procedure with some passed values.

Not a master of SQL, so I sketched out what I need in C# instead:

Table createAndFillTable(string tableName, string[] columnNames, string[] columnTypes, string[] rows)
{
    var t = CreateNewTable(tableName);
    for(int i = 0; i < columnNames; i++)
    {
        t.addColumn(columnNames[i], columnTypes[i]);
    }

    foreach(var r in rows)
    {
        //Needs parsing to correct data types
        t.AddRow(r);
    }

    return t;
}

The example is with creating new tables, but the optimal solution would be to just import to an existing table by passing 2 parameters, tableName and fileLocation and let the sql handle the variable types etc. No idea if that's a better solution though.

Thanks up front!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source