declare @.execution_string varchar(1000),
@.email_id varchar(500),
@.filename varchar(200),
@.reportname varchar(100),
@.osql_query varchar(1000),
@.fieldname varchar(8000),
@.fieldnametemp varchar(100),
@.query varchar(8000),
@.seperator1 varchar(2),
@.seperator2 varchar(2)
select @.fieldname =''
select @.fieldnametemp = ''
exec sp_serveroption [myserver] , 'data access', 'true'
set nocount on
declare reports_scheduled cursor for
select 'exec mydb.dbo.'+a.parameter_string , b.report_name from report_schedule a , report_master b
where a.Active='Y'
AND a.frequency like '%-'+SUBSTRING(datename(weekday,getDate()),0,3)+'-%' and a.report_no = b.report_no
open reports_scheduled
fetch next from reports_scheduled into @.execution_string,@.reportname
while @.@.fetch_status = 0
begin
declare @.l_str nvarchar(555)
select @.l_str ="CREATE procedure temp_proc as select top 100 * into temp_table from openquery ([myserver], """ + @.execution_string + """)"
exec (@.l_str)
exec temp_proc
select @.filename = 'c:\' + @.reportname+ convert(varchar(10),getdate(),112)+'.csv'
select @.filename= rtrim(ltrim(replace(@.filename,' ','')))
declare fnames cursor for
select name from syscolumns where id in (select id from sysobjects where name = 'temp_table')
open fnames
fetch next from fnames into @.fieldnametemp
while @.@.fetch_status = 0
select @.seperator1 = '"'
select @.seperator2 = '" '
begin
select @.fieldname = '''"'''+'+convert(varchar,replace(['+@.fieldnametemp + '],'+@.seperator1+','+@.seperator2+'))+'+ '''"'''+ ','+ @.fieldname
fetch next from fnames into @.fieldnametemp
end
close fnames
deallocate fnames
select @.fieldname = left(@.fieldname,len(@.fieldname)-1)
select @.fieldname
select @.query = 'echo select '+@.fieldname+ ' from temp_table > c:\temp_query.txt'
select @.query
exec master..xp_cmdshell @.query
select @.osql_query = 'osql -E -d mydb -i c:\temp_query.txt -s , -n -w 10000 -o '+ @.filename + ' -c'
select @.osql_query
exec master..xp_cmdshell @.osql_query
exec master..xp_sendmail @.recipients = '123@.123.com
,@.message ='This is a system generated mail. Please do not reply to this mail'
,@.attachments = @.filename
,@.subject = @.reportname
drop table temp_table
drop procedure temp_proc
fetch next from reports_scheduled into @.execution_string ,@.reportname
end
close reports_scheduled
deallocate reports_scheduled
The reults from any sp are taken and send to a given mail address by mail in excel format
This procedure gives me a error when i try to run it
Server: Msg 7391, Level 16, State 1, Procedure temp_proc, Line 1
[Microsoft][ODBC SQL Server Driver][SQL Server]The operation could not be performed because the OLE DB provider 'SQLOLEDB' was unable to begin a distributed transaction.
[OLE/DB provider returned message: New transaction cannot enlist in the specified transaction coordinator. ]
OLE DB error trace [OLE/DB Provider 'SQLOLEDB' ITransactionJoin::JoinTransaction returned 0x8004d00a].
DTC is up and running and no problems in that.
Any idea what might be causing this error .
It was running earlier and changes I made in the server were enabling replicationselect @.l_str ="CREATE procedure temp_proc as select top 100 * into temp_table from openquery ([myserver], """ + @.execution_string + """)"
exec (@.l_str)
exec temp_proc
exec temp_proc
What do you expect this to do?
Besides being bad practice to create SPs like this - why have you made it an SP when it looks like it could be dynamic sql.
execing it twice will give an error as will running your procedure again if it fails after the creation and before the drop.
Tables and SPs are meant to be permaneny objects and not created/destroyed dynamically.
If you need to do something like this I would recommend a global temp table - and as your structure does not change create it once outside the loop (using and 1=0 appended to the where clause) and truncate it before populating with an insert inside the loop.
I'm guessing your error is due to the provider for myserver whatever that is. If it's a self linked server check how it's set up.|||Tables and SPs are meant to be permaneny objects and not created/destroyed dynamically.
If you need to do something like this I would recommend a global temp table - and as your structure does not change create it once outside the loop (using and 1=0 appended to the where clause) and truncate it before populating with an insert inside the loop.
Thats the whole point ... I do not know the structure of the temp table ... its the resultset returned by the stored procedure executed in the openquery part.|||while @.@.fetch_status = 0
select @.seperator1 = '"'
select @.seperator2 = '" '
begin
select @.fieldname = '''"'''+'+convert(varchar,replace(['+@.fieldnametemp + '],'+@.seperator1+','+@.seperator2+'))+'+ '''"'''+ ','+ @.fieldname
fetch next from fnames into @.fieldnametemp
end
found the error
Showing posts with label varchar. Show all posts
Showing posts with label varchar. Show all posts
Sunday, February 26, 2012
f1 -- need help
Labels:
database,
declare,
email_id,
execution_string,
filename,
microsoft,
mysql,
oracle,
osql_query,
reportname,
server,
sql,
varchar
Sunday, February 19, 2012
Extracting image column to the file
Hello all,
how can I extract image strored in DB in to file?
Will something like this work?
DECLARE @.cmd VARCHAR(2048)
SET @.cmd = 'bcp select history_image from recipient where recipient_id = 1
queryout c:\test\625954.jpg '
EXEC master..xp_cmdshell @.cmd, NO_OUTPUTYou want to use TextCopy.
e.g.
--OJ: TEXTCOPY example
-- Loading txt files into db
--create tb to hold data
create table tmp(fname varchar(100),txt text default '')
go
declare @.sql varchar(255),
@.fname varchar(100),
@.path varchar(50)
--specify desired folder
set @.path='c:\test'
set @.sql='dir ' + @.path + '*.txt /c /b'
--insert filenames into tb
insert tmp(fname)
exec master..xp_cmdshell @.sql
--loop through and insert file contents into tb
declare cc cursor
for select fname from tmp
open cc
fetch next from cc into @.fname
while @.@.fetch_status=0
begin
set @.sql='textcopy /s"dev" /d"tempdb" /t"tmp" /c"txt" /w"where
fname=''' + @.fname + '''"'
set @.sql=@.sql + ' /f"' + @.path + @.fname + '" /i'
print @.sql
exec master..xp_cmdshell @.sql,no_output
fetch next from cc into @.fname
end
close cc
deallocate cc
go
select * from tmp
go
drop table tmp
go
-oj
"Gene" <Gene@.discussions.microsoft.com> wrote in message
news:DDDCCF4D-23B1-4348-9740-47A7738C9E36@.microsoft.com...
> Hello all,
> how can I extract image strored in DB in to file?
> Will something like this work?
> DECLARE @.cmd VARCHAR(2048)
> SET @.cmd = 'bcp select history_image from recipient where recipient_id = 1
> queryout c:\test\625954.jpg '
> EXEC master..xp_cmdshell @.cmd, NO_OUTPUT
how can I extract image strored in DB in to file?
Will something like this work?
DECLARE @.cmd VARCHAR(2048)
SET @.cmd = 'bcp select history_image from recipient where recipient_id = 1
queryout c:\test\625954.jpg '
EXEC master..xp_cmdshell @.cmd, NO_OUTPUTYou want to use TextCopy.
e.g.
--OJ: TEXTCOPY example
-- Loading txt files into db
--create tb to hold data
create table tmp(fname varchar(100),txt text default '')
go
declare @.sql varchar(255),
@.fname varchar(100),
@.path varchar(50)
--specify desired folder
set @.path='c:\test'
set @.sql='dir ' + @.path + '*.txt /c /b'
--insert filenames into tb
insert tmp(fname)
exec master..xp_cmdshell @.sql
--loop through and insert file contents into tb
declare cc cursor
for select fname from tmp
open cc
fetch next from cc into @.fname
while @.@.fetch_status=0
begin
set @.sql='textcopy /s"dev" /d"tempdb" /t"tmp" /c"txt" /w"where
fname=''' + @.fname + '''"'
set @.sql=@.sql + ' /f"' + @.path + @.fname + '" /i'
print @.sql
exec master..xp_cmdshell @.sql,no_output
fetch next from cc into @.fname
end
close cc
deallocate cc
go
select * from tmp
go
drop table tmp
go
-oj
"Gene" <Gene@.discussions.microsoft.com> wrote in message
news:DDDCCF4D-23B1-4348-9740-47A7738C9E36@.microsoft.com...
> Hello all,
> how can I extract image strored in DB in to file?
> Will something like this work?
> DECLARE @.cmd VARCHAR(2048)
> SET @.cmd = 'bcp select history_image from recipient where recipient_id = 1
> queryout c:\test\625954.jpg '
> EXEC master..xp_cmdshell @.cmd, NO_OUTPUT
Friday, February 17, 2012
extracting data from a varchar variable?
Guys.
I have some data like this one:
'Uploadfiles\CompanyID\ProjectCode\Files\File1.doc'
P.S: the values may vary each time.
I need to programmatically extract from that kind of string only file1.doc
Ho can I dynamically do it? any function? statement? etc? please help.
The idea is getting only the string after the last "\"
Thanks
Frdeclare @.t varchar(256)
select @.t = 'Uploadfiles\CompanyID\ProjectCode\Files\File1.doc'
select reverse(substring(reverse(@.t),1,patindex('%\%',reverse(@.t))-1))
Labels:
companyid,
database,
doc,
extracting,
file1,
files,
guys,
microsoft,
mysql,
oracle,
programmatically,
projectcode,
server,
sql,
time,
uploadfiles,
values,
varchar,
variable,
vary
Subscribe to:
Posts (Atom)