Technical Article

SP to import to a file

,

Is a SP that imports a table to a file. You just put the table name, the path and the sa password and then you have it. It is important that it parse the table inserted (pubs.dbo.sales for example) and checks that the db, the table with the owner exists

/*
**Author Rodrigo Acosta
**Email acosta_rodrigo@hotmail.com
*/Create proc proc_imp
@table Varchar(70)=Null,
@file Varchar(100)=NUll,
@password Varchar(20)=''
As
Set Nocount On

/*
**The table name and the file path can`t be null,
**so I check it, and tell it if so
*/If @table Is Null or @file is Null
Begin
Print 'The Table name or the file path can`t be null.'
Print 'Run proc_imp again.'
Print 'Use: EXEC proc_imp @table="pubs.dbo.sales",
@file="c:\MSSQL7\Data\pubs.imp",
@password="Hornet"'
Return
End

/*
**The table name must by in this way db.owner.table
**So I check that is inserted right
*/If @table not Like '%.%.%'
Begin
Print 'The name of the table is wrong. The correct sentence is '
Print '"Database_Name.Owner.Table_name". Example: Northwind.dbo.sales'
Return
End

/*
**The filename must contain the path in this format c:\Whatever\File.ext
**So I check it
*/If @file Not Like '_:\%'
Begin
Print 'The filename inserted is wrong. Include the path where the file is.'
Print 'For Example: C:\Imports\Pubs.imp '
Return
End

/*
**I need to check that the object exists. I parse the @table value
**in database and table and check their existance
*/Declare @db varchar(100)
Declare @tb Varchar(100)
--First the Database
Set @db=(Select parsename(@table,3))-- in @parse I have the database name
If Not Exists (
Select * from master.dbo.sysdatabases
Where name=@db
)
Begin
Print 'The database name inserted in "'+@table+'" doesn'+''''+'exists'
Print 'Re-enter the name.'
Return
End
--Now the Table 
Declare @select Varchar(200)
Declare @tb2 varchar(50)


Set @tb=(Select parsename(@table,1))--@tb= the name inserted
Set @select='(Select name from '+@db+'.dbo.sysobjects where name="'+@tb+'")'

Create table #tb
(name Varchar(50))

Insert #tb
EXEC (@select)

Set @tb2=(Select name from #tb) --@tb2= The real name of the table. If it is null, then doesn' exists.
Drop table #tb

If @tb2 is null Begin
Print 'The table name inserted in "'+@table+'" doesn'+''''+'exists'
Print 'Re-enter the name.'
Return
End

--Now The owner
Declare @owner varchar(50)
Declare @owner2 Varchar(50)
Set @owner=(Select parsename(@table,2)) --@owner= the owner inserted
Set @Select='select o.name, u.name
from '+@db+'.dbo.sysobjects o inner join '+@db+'.dbo.sysusers u
On o.uid=u.uid
where o.name="'+@tb+'"'

Create Table #owner
(tname varchar(50),
uname varchar(50))

Insert #owner
EXEC (@select)

Set @owner2=(Select uname from #owner)
Drop table #owner

If @owner<>@owner2
Begin
Print 'The owner of table '+@table+' doesn'+''''+'t exists.'
Print 'Re-enter the name.'
Return
End

/*
**the sintax that executes the bcp program
*/Declare @cmd Varchar(1000)
Set @cmd='EXEC master.dbo.xp_cmdshell '+''''+'BCP '+@table+' in '+@file+' -n -Usa -P'+@password+''''

EXEC (@cmd)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating