I am trying to create table from stored procedure, but while executing the procedure, I am getting these errors:
Msg 343, Level 15, State 1, Line 1
Unknown object type 'TABLEPunith_INC_T_Partner_PlusEligible_YE_APJC' used in a CREATE, DROP, or ALTER statement.
Msg 4701, Level 16, State 1, Line 1
Cannot find the object "PTEST" because it does not exist or you do not have permissions.
This is my stored procedure
ALTER PROCEDURE [dbo].[SP_PTEST2]
AS
BEGIN
DECLARE @OBJECTNM NVARCHAR(100)
DECLARE @SQLSTR NVARCHAR(MAX)
SET @OBJECTNM = 'PTEST'
IF (EXISTS (SELECT 1 FROM SYS.OBJECTS WHERE NAME = 'PTEST'))
BEGIN
SET @SQLSTR = 'DROP TABLE ' + @OBJECTNM
EXEC(@SQLSTR)
END
SET @SQLSTR = 'CREATE TABLE' + @OBJECTNM + '(
Country VARCHAR(255),
BE_Geo_ID INT,
BE_Geo_Name VARCHAR(255),
Certification VARCHAR(200),
)'
EXEC(@SQLSTR)
SET @SQLSTR = 'TRUNCATE TABLE ' + @OBJECTNM
EXEC(@SQLSTR)
SET @SQLSTR = 'BULK INSERT ' + @OBJECTNM +
' FROM ''C:\Desktop\Part plus.csv''
WITH (
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n'',
FIRSTROW = 2
)'
EXEC(@SQLSTR)
End
I spent hours on this but I am unable execute the stored procedure, also when I tried with different type procedure is executed but table is not created.
Somebody please help me on this.
Thanks in Advance.
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!