求编写一个存储过程,急用
我有个表 HireInfo 其中有字段TelRegTime ,datetime类型
现在想把HireInfo表的数据在当TelRegTime 在某两个时间段导入到文本中,当时间没有设置的时候默认是当前时间为止的所有数据。
CREATE PROCEDURE [dbo].[File2TableOut]
as
exec( 'master..xp_cmdshell ' 'bcp "Select * from ksga.dbo.HireInfo " queryout "c:\hireinfo.txt " -c -t "| " -S "(local) " -U sa -P tonghu ' ' ')
GO
现在问题就出在怎么加两个时间变量把它连起来。请高手帮忙,急用
------解决方案--------------------Select * from ksga.dbo.HireInfo where TelRegTime between datetime1 and datetime2
------解决方案----------------------創建
CREATE PROCEDURE [dbo].[File2TableOut] (@StartTime DateTime, @EndTime DateTime)
as
Declare @S Varchar(8000)
Set @S= 'bcp "Select * from ksga.dbo.HireInfo Where TelRegTime Between ' ' ' + Convert(Varchar, @StartTime, 120) + ' ' ' And ' ' ' + Convert(Varchar, @EndTime, 120) + ' ' ' " Queryout "C:\hireinfo.txt " -c -t "| " -S "(local) " -U sa -P tonghu ' ' '
Exec master..xp_cmdshell @S
GO
--調用
EXEC File2TableOut '2007-06-11 ', '2007-06-13 '
------解决方案--------------------if exists(Select 1 from ksga.dbo.HireInfo where where TelRegTime between @datetime1 and @datetime2)
begin
exec( 'master..xp_cmdshell ' 'bcp "Select * from ksga.dbo.HireInfo " queryout "c:\hireinfo.txt " -c -t "| " -S "(local) " -U sa -P tonghu ' ' ')
end
------解决方案--------------------CREATE PROCEDURE [dbo].[File2TableOut]
datetime1 datetime,datetime2 datetime
as
if datetime2= ' ' then set datetime = getdate()
Select * into #lsb from ksga.dbo.HireInfo where TelRegTime between datetime1 and datetime2
exec( 'master..xp_cmdshell ' 'bcp "Select * from #lsb " queryout "c:\hireinfo.txt " -c -t "| " -S "(local) " -U sa -P tonghu ' ' ')
drop table #lsb
GO
------解决方案--------------------搞明白了就结贴呀 我也混点分~~~:)
------解决方案--------------------up
------解决方案--------------------mark