开发者

cannot we pass the table as a parameter in stored procedure..?

开发者 https://www.devze.com 2023-01-03 22:10 出处:网络
SET QUOTED_IDENTIFIER OFF go CREATE PROCEDURE ps_StudentList_Import @PathFileName varchar(100), @tablename varchar(100)
SET QUOTED_IDENTIFIER OFF
go
CREATE PROCEDURE ps_StudentList_Import
@PathFileName varchar(100),
@tablename varchar(100)

AS

DECLARE @SQL varchar(2000)

 BEGIN

  SET @SQL = "BULK INSERT '"+@tablename+"' FROM '"+@PathFileName+"' WITH (FIELDTERMINATOR = '"",""',ROWTERMINATOR = '""\n""') "
 END
开发者_如何学Go exec(@SQL);
 go


 EXEC ps_StudentList_Import 'c:\data\DimActivity.txt' ,'dbo.DimActivity';

here the 'dbo.DimActivity' is a table as parameter cannot we pass the table as a parameter? what is wrong with my above code..please anyone help me out...


Placing begin and end at the right place might help:

AS
    BEGIN
    DECLARE @SQL varchar(2000)
    SET @SQL = "BULK INSERT '"+@tablename+"' FROM '"+@PathFileName+
        "' WITH (FIELDTERMINATOR = '"",""',ROWTERMINATOR = '""\n""') "
    exec(@SQL);
    END
go


I'm not sure about the quotes usage. Does this work?

CREATE PROCEDURE ps_StudentList_Import
@PathFileName varchar(100),
@tablename sysname

AS
BEGIN
DECLARE @SQL varchar(2000)

 SET @SQL = 'BULK INSERT '+@tablename+' FROM '''+@PathFileName+''' WITH (FIELDTERMINATOR = '','',ROWTERMINATOR = ''\n'') '

print @SQL
exec(@SQL);
 END


You could try dropping the "dbo." from table name. The dbo is just a namespace, the actual name is "DimActivity". Add a 'use' statement at the top to specify which database the table is in.

0

精彩评论

暂无评论...
验证码 换一张
取 消