Create a table using script

  • Hi,

    I have a create table script as .txt file. How can i create table using that script through sql query. Plz any one suggest some approach. I heard through openrow set it can be achieved. But how to do.

    Thanks

  • I tried it by writting below code

    CREATE TABLE #TEMP

    (

    SCRIPT VARCHAR(4000)

    )

    INSERT INTO #TEMP

    EXEC XP_CMDSHELL 'type filefath\filename.txt'

    DECLARE @sql VARCHAR(8000)

    SELECT @sql= STUFF((

    SELECT CONVERT(VARCHAR(1000),' '+ SCRIPT) FROM #TEMP

    FOR XML PATH('')),1,1,'')

    EXEC (@SQL)

    DROP TABLE #temp

  • Why try to open it through a SQL query? Why not just use a command line utility like PowerShell or sqlcmd.exe? Either of those can read the file and execute it against a database. That's much more efficient than trying to open files through T-SQL.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • ahhh :crazy:... Indeed... Silly me ... Please try doing the way Grant has proposed...

  • Thanks for your reply is there any way to achieve this using open row set

  • I'm not sure. It's not something I would try to do. What's the purpose of this approach?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • I guess vinodhkumargv is looking for simple solution and could be...

    Copy .txt code and past it to the new query windows and execute it. It will create the table.

    HTH

    ---------------------------------------------------
    "Thare are only 10 types of people in the world:
    Those who understand binary, and those who don't."

  • :-D:-):Wow:

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply