| Author | Topic | 
                            
                                    | MaysamStarting Member
 
 
                                        8 Posts | 
                                            
                                            |  Posted - 2009-07-02 : 16:56:56 
 |  
                                            | Hey guys, I want to create an Excel file for each record inserted into a table. Therefore, I decided to use Xp_cmdShell extended stored procedure. However, I faced a very strange problem .It seems that Xp_cmdshell does not work with variables.This is my code :--------------------------------------------------------------------set @l_param = 'Execute master..xp_cmdshell' + char(39)+' bcp  "Select * from TblName where integrationID='+ rtrim(@l_ID) +'" queryout '+rtrim(@l_genFileName)+' -U username -P password -c '+ char(39)execute sp_executesql @l_param--select @l_param--------------------------------------------------------------------This command does not work , I do not recieve any error message, it just takes for ever. but when I retrieve the command (@l_param) , copy the command and execute it , it works. Any ideas how to fix this problem ?Thank you. |  | 
       
                            
                       
                          
                            
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 01:38:56 
 |  
                                          | set @l_param = ' bcp "Select * from TblName where integrationID='+ rtrim(@l_ID) +'" queryout '+rtrim(@l_genFileName)+' -U username -P password -c '+ char(39)Execute master..xp_cmdshell @l_param No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 02:32:24 
 |  
                                          | First print @l_param and see the resultMadhivananFailing to plan is Planning to fail |  
                                          |  |  | 
                            
                       
                          
                            
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 02:45:51 
 |  
                                          | quote:Originally posted by madhivanan
 First print @l_param and see the resultMadhivananFailing to plan is Planning to fail
 
 quote:It is already done.Fredbut when I retrieve the command (@l_param) , copy the command and execute it , it works.
 
 No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | madhivananPremature Yak Congratulator
 
 
                                    22864 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 02:51:50 
 |  
                                          | quote:It is already done.FredOriginally posted by webfred
 
 quote:Originally posted by madhivanan
 First print @l_param and see the resultMadhivananFailing to plan is Planning to fail
 
 quote:but when I retrieve the command (@l_param) , copy the command and execute it , it works.
 
 No, you're never too old to Yak'n'Roll if you're too young to die.
 Thanks. I forgot to note that
  MadhivananFailing to plan is Planning to fail |  
                                          |  |  | 
                            
                       
                          
                            
                                    | MaysamStarting Member
 
 
                                    8 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 12:07:58 
 |  
                                          | @webfred : Xp_CmdShell does not accept any variables therefore I have to use sp_ExecuteSQL@Madhivana :   First print @l_param and see the resultI did that , the command works correctly. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | MaysamStarting Member
 
 
                                    8 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 12:10:48 
 |  
                                          | It seems there are some sort of permission problem . |  
                                          |  |  | 
                            
                       
                          
                            
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 15:16:58 
 |  
                                          | quote:Execute master..xp_cmdshell @l_paramSomething like this above I have already used and it worked very well.Why do you mean that xp_cmdshell does not accept any variables?Originally posted by Maysam
 @webfred : Xp_CmdShell does not accept any variables therefore I have to use sp_ExecuteSQL
 
 No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | MaysamStarting Member
 
 
                                    8 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 15:31:50 
 |  
                                          | quote:I meanExecute master..xp_cmdshell ' bcp "Select * from TblName where integrationID='+ rtrim(@l_ID) +'" queryout '+rtrim(@l_genFileName)+' -U username -P password -c 'will not work , I mean variable in the body of command , @l_param is one parameter , that's fine but you can not create the command using prameters , that will not work.Originally posted by webfred
 
 quote:Execute master..xp_cmdshell @l_paramSomething like this above I have already used and it worked very well.Why do you mean that xp_cmdshell does not accept any variables?Originally posted by Maysam
 @webfred : Xp_CmdShell does not accept any variables therefore I have to use sp_ExecuteSQL
 
 No, you're never too old to Yak'n'Roll if you're too young to die.
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 15:55:19 
 |  
                                          | Hello Maysam,maybe it is the heat and my brain is cooked.But one time again.I have recently used:declare a variable Built command string in that variableUse it: Execute master..xp_cmdshell @reay_built_variableAlso see:http://weblogs.sqlteam.com/tarad/archive/2004/03/29/1129.aspxFred No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | MaysamStarting Member
 
 
                                    8 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 16:09:57 
 |  
                                          | :) Ok try these two commands1 - declare @param varchar(100)Set @param = 'I love 4th of July'execute master..xp_CmdShell 'echo '+@param2 -execute master..xp_CmdShell 'echo I love 4th of July'The first coomand will not work |  
                                          |  |  | 
                            
                       
                          
                            
                                    | MaysamStarting Member
 
 
                                    8 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 16:10:53 
 |  
                                          | Sorry, I checked the link late :DThat's what I mean :D |  
                                          |  |  | 
                            
                       
                          
                            
                                    | webfredMaster Smack Fu Yak Hacker
 
 
                                    8781 Posts | 
                                        
                                          |  Posted - 2009-07-03 : 16:28:23 
 |  
                                          | OK  I cannot see the problem in building the complete string in a variable and passing it to the xp_cmdshell  For example set @param='echo '+@paramBut anywayWish you to have a very nice 4th of JulyFred No, you're never too old to Yak'n'Roll if you're too young to die.
 |  
                                          |  |  | 
                            
                            
                                |  |