| Author |
Topic |
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-10 : 10:07:45
|
| Hello !I'm new to this forum ;I'm new to SQL Server 2005 programming to. I guess I could find some help about my issue.Here my environnment :I have a production software that allow to use DSN to connect to database ; with a proper DSN, I can populate a table on my SQL2005 server. What I need to achieve, is to get the just inserted data, and dump them into a file for further processing.I created a trigger that runs "after insert" on the needed table. This trigger make external directory creation, and dump the data (from the selected table) into a file ; I use the "xp_cmdshell" command to echo data into the file.When I make a query as 'sa', on the SQL management tools, everything work as expected. When I use the production software, it seems that the select query I make (@select data = select field1+'|'+ field2 from inserted) seems to return NULL (I traced the values). This behaviour appends even if I use "sa" user on the DSN connection.I hope someone can help me :)regards,Raphael. |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-10-10 : 10:31:13
|
| If you're running in xp_cmdshell then it will create a new connection and the inserted table will not be available.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
lappin
Posting Yak Master
182 Posts |
Posted - 2011-10-10 : 10:37:24
|
| Is it just a typo or is (@select data = select field1+'|'+ field2 from inserted) your real query? Should it not be select @data = (select field1+'|'+ field2 from inserted)Are there any NULLs? Appending a NULL equals NULL. You can use ISNULL to replace nulls with empty string.select @data = (select ISNULL(field1,'')+'|'+ ISNULL(field2,'') from inserted) |
 |
|
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-10 : 10:53:40
|
Hi There,Thx for yours answers !regarding the typo, the real query is longer ; I just concatenate all the field, depending on their type. It doesn t matter if field are NULL or not.Regarding the new connection, before calling the xp_cmdshell, I "save" the inserted data into a variable (the select @data = select bla from selected) statement). So I just call the xl_cmdshell and provide a builded command shell :SET @CMD='echo "'+@data+'">'@PATH + @FILENAME;EXEC xp_cmdshell @CMD,NO_OUTPUT; regards, |
 |
|
|
lappin
Posting Yak Master
182 Posts |
Posted - 2011-10-11 : 05:36:18
|
| You can break process into the two parts to find problem.1. Modify trigger to send output from INSERTED into a test table instead of to file, e.g. Select * into Test1 from insertedWhat is in table Test1?2. Modify trigger to send fixed data instead of contents of INSERTED to output file, e.g. SET @data='testvalue1'+'|'+'testvalue2'SET @CMD='echo "'+@data+'">'@PATH + @FILENAME;EXEC xp_cmdshell @CMD,NO_OUTPUT;what is in file?Which part failed? |
 |
|
|
lappin
Posting Yak Master
182 Posts |
Posted - 2011-10-11 : 09:42:59
|
| I tested this and the following saves hard-coded values:declare @Data varchar(8000),@Path varchar(8000),@CMD varchar(8000),@FILENAME varchar(8000)set @PATH ='C:\temp\'set @FILENAME ='text.txt'SET @data='"' + 'testvalue1'+'|'+'testvalue2' +'"'SET @CMD='echo '+@data + ' >' +@PATH + @FILENAME ;print @CMDEXEC xp_cmdshell @CMD,NO_OUTPUT;If this works in trigger, replace static values with contents of INSERTED |
 |
|
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-11 : 09:59:30
|
| I really appriciate all your help !I've wrote some "log stuff", to monitor what is going on ; I make some insert in another table with variable content ; the query from the "inserted" table provide no data (NULL) when the trigger is fired from the external software (using DNS) and it provide the expected data when I fire the trigger from the SQL Management studio window.I'm not at my office this afternoon, I will provide you all the trigger's code tomorrow.Again, thx for your support :) |
 |
|
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-12 : 03:45:38
|
Here is the trigger snippet code :My environnement : SQLServer 2005, PV_TEST2 is the database; it contains 2 tables, results and debugTraceDSN is like : DRIVER={SQL Server};DATABASE=PV_TEST2;SERVER=FQDNofTheServer;UID=PV_sun;PWD=****USE [PV_TEST2]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TRIGGER items_insert on ResultsAFTER INSERT ASBEGINSET XACT_ABORT ONSET REMOTE_PROC_TRANSACTIONS OFF DECLARE @OUTPUT AS VARCHAR(8000);DECLARE @OUTPUT2 AS VARCHAR(8000);DECLARE @CMD as VARCHAR(8000);DECLARE @SEPARATOR as VARCHAR(5);DECLARE @SERVERPATH as varchar(100);DECLARE @BATCHFILE as varchar(100);-- Variable for folder nameDECLARE @TodayDate as varchar(40);DECLARE @TodayHour as varchar(40);DECLARE @TodayMinu as varchar(40);DECLARE @TodaySec as varchar(40);DECLARE @TodayMSec as varchar(40);DECLARE @NewFileName as varchar(100);DECLARE @GenericFileName as varchar(100);-- INIT VariableSELECT @TodayDate = CONVERT(varchar(10), GETDATE(), 112)SELECT @TodayHour = DATEPART(hh,GETDATE())SELECT @TodaySec = DATEPART(ss,GETDATE())SELECT @TodayMinu = DATEPART(mi,GETDATE())SELECT @TodayMSec = DATEPART(ms,GETDATE())SELECT @NewFileName =@TodayDate + '_' + @TodayHour + '_' + @TodayMinu + '_'+ @TodaySec + '_' + @TodayMSec-- Other variable, for file creationSELECT @SEPARATOR='|';SELECT @SERVERPATH='e:\PV\';SELECT @BATCHFILE='uploadPV.bat';SELECT @GENERICFILENAME='output.dat'SET XACT_ABORT ONSET REMOTE_PROC_TRANSACTIONS OFF-- @OUTPUT should receive the concatenation of the data separated with a pipeSELECT @OUTPUT = (SELECT left(SerialNumber,30) +@SEPARATOR+ ModuleName +@SEPARATOR+ convert(varchar,Reduction,120) FROM inserted)-- This insert is for debug ; when the trigger is launch by hand with INSERT Statement, the output is good, as expected-- When the trigger is fired from the application using DSN, this output is NULLINSERT DebugTrace SELECT 'PV', USER_NAME(), getdate(), 'OUTPUT '+@OUTPUTSET @CMD='mkdir ' +@SERVERPATH+@NewFileName-- In all case, the folder is properly createdEXEC xp_cmdshell @CMD,NO_OUTPUT;-- Debug insert ; always with expected valuesINSERT DebugTrace SELECT 'PV', USER_NAME(), getdate(), 'MKDIR '+@CMDSET @CMD='echo "'+@OUTPUT+'">'+@SERVERPATH+@NewFileName+'\'+@GENERICFILENAME-- Here, again, manual insert : OK, "DSN insert" : NOK, @CMD is NULLINSERT DebugTrace SELECT 'PV', USER_NAME(), getdate(), 'ECHO '+@CMDEXEC xp_cmdshell @CMD,NO_OUTPUT;-- batch call this parametersSELECT @BATCHFILE=@SERVERPATH+@BATCHFILE+' '+@SERVERPATH+' '+@NewFileName +' '+ @GENERICFILENAME-- batch always runEXEC xp_cmdshell @BATCHFILE;INSERT DebugTrace SELECT 'PV', USER_NAME(), getdate(), 'BATCHFILE '+@BATCHFILEEND |
 |
|
|
lappin
Posting Yak Master
182 Posts |
Posted - 2011-10-12 : 05:01:52
|
| When you send data to the debug table - "this output is NULL" Is a new row inserted with null values? |
 |
|
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-12 : 05:33:57
|
Hello Lappin !Yes, strange stuff :This line INSERT DebugTrace SELECT 'PV', USER_NAME(), getdate(), 'OUTPUT '+@OUTPUT Provide NULL for the last field, even if 'OUTPUT 'string is concatenated this @OUTPUT value.RAF |
 |
|
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-12 : 08:24:20
|
| This strange 'NULL' string make me think that maybe the concatenation I've done fails somewhere, then, the query fails, but without error message. So I remove almost all the concatenated field, and then it works !I have to investigate why and which field fails !There is a lot of float field (almost 45) that I cast to get an string .. issue may come from there.I will keep you updated.Raphael. |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2011-10-12 : 08:36:21
|
quote: Originally posted by elifrance ... It doesn t matter if field are NULL or not.
It does if your concatenating them.-----------What color do you want that database? |
 |
|
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-12 : 09:38:52
|
| Hi elwoos !Is there an easy way to check if field is NULL in the select statement ? Otherwise, I will have to check every 50 fields of the tables ... |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2011-10-12 : 10:39:14
|
| As Lappin suggested early on in this thread you can use ISNULL which you would need for any field that allows NULL entries, though you may want to consider the alternative of not allowing NULL values in your data.-----------What color do you want that database? |
 |
|
|
elifrance
Starting Member
8 Posts |
Posted - 2011-10-14 : 04:19:42
|
| Hi guys,the issue is solve and came from the concatenated NULL value ; By testing each of them in my select query, I managed to make it work !Thx too all for you help !RAF |
 |
|
|
|