What I have
I am currently inserting all the SQL files from the current directory in a temporary table :
create table #tmpSQLFiles(SQLFileName varchar(max))
set @SqlStatement = 'master.dbo.xp_cmdshell ''dir /b "' + @DirPath + '*.sql"'''
insert into #tmpSQLFiles
execute (@SqlStatement)
This works well.
What I want
Now I want the table to have an extra varchar column in which I can store a certain desired string :
create table #tmpSQLFiles(SQLFileName varchar(max), MyString varchar(max))
set @SqlStatement = 'master.dbo.xp_cmdshell ''dir /b "' + @DirPath+ '*.sql"'''
What I have tried
How can I do the insert? I have tried :
insert into #tmpSQLFiles (SQLFileName, MyString) values (execute (@SqlStatement), 'aa')
or :
declare @ExecutedStmt varchar(max)
set @ExecutedStmt = execute (@SqlStatement)
-- But the above line is not correct.
Any of these 2 would work for me. I don’t want to insert and update afterwards the second column.
Any ideas on how I can achieve this? Thank you !
Try using a table variable as an intermediary – not the prettiest, but does the job:
If you weren’t using an extended stored procedure (
xp_cmdshell), you might have been able to useOPENROWSETto execute the SP: