动态sql存储过程更新查询问题?

时间:2012-12-06 09:36:53

标签: sql sql-server sql-server-2008 stored-procedures dynamic-sql

我编写了以下代码,将表格中的filepath列设置为'F:\DataMigration\Wise\DELTA_20121008\Attachments\SR\SR_1.txt'

where SR_1 is file_name column
      .txt is file_ext column from my table.

但是在执行了以下过程之后,我将表格中的filepath列作为

'F:\DataMigration\Wise\DELTA_20121008\Attachments\file_name.file_ext'

表示它将列名称视为字符串,我如何将其作为列进行处理 使用该列中的值。

alter procedure [dbo].[WISEMissingAttachmentReportGenerator]
(
@tablename varchar(255), @pathonlocal nvarchar(255)
)
as
begin
--step 1
exec dbo.proc_alter_table @tablename
--step 2
EXEC ('update '+ @tablename +
' set filepath = '''+ @pathonlocal + ' file_name'+'.'+'file_ext''')
EXEC('Select * from '+@tablename)
end

exec [dbo].[WISEMissingAttachmentReportGenerator] [WISE.Non_VP_Service_Request_Attachments_File_Check_Analysis],
N'F:\DataMigration\Wise\DELTA_20121008\Attachments\SR\'

3 个答案:

答案 0 :(得分:1)

尝试将您的陈述更改为:

EXEC ('update '+ @tablename +
' set filepath = '''+ @pathonlocal + ''' + file_name + ''.'' + file_ext')

答案 1 :(得分:1)

尝试;

EXEC('UPDATE '+ @tablename +
' SET filepath = '''+ @pathonlocal + ''' + file_name + '''+'.'+''' + file_ext')

等于;

UPDATE [YourTable] SET filepath = 'YourPath' + file_name + '.' + file_ext

答案 2 :(得分:0)

declare @tblnm varchar(20) = 'test'
declare @upda varchar(20) = 'update '
declare @set varchar(25) = ' set'
declare @id varchar(25) = ' id'
declare @cmd varchar(1000)

set @cmd = @upda + @tblnm + @set + @id + '=7'
exec(@cmd)

SAMPLE SQL UPDATE QUERY - FOR BUILDING TABLENAME DYNAMICALLY  
EXECUTED GUYS - THIS IS CALL JUGAAAAAAAAAD [NO NEED TO GET INTO ''' STUFF]