Create Table dynamically in sql server

I'd like to create a stored procedure that takes the name of table as a parameter, and then creates that table, if it doesn't exist, according to a schema stored somewhere.

Is that possible-- Can I store a schema and use is as a template to create tables from? Or must I hard-code the CREATE TABLE and its scheme in the stored procedure itself?

The idea here is to be able to change the schema without changing the stored procedure (separation of concerns), because I'm probably going to expand the procedure to do more.

This is as far as I got without knowing whether to just hard-code the schema for the table in the sproc, or whether I can store and pull that schema somehow.

CREATE PROCEDURE GenerateTable
@tableName as nvarchar(128)
AS
BEGIN
IF OBJECT_ID(@tableName,'U') IS NULL
CREATE TABLE @tableName ????????
ENDIF
END
GO

No comments: