Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: Dynamic Select Statement


Message #1 by "Frankie, Loh Teck Seng" <lohts@p...> on Mon, 20 Nov 2000 11:57:45 +0800
Greeting To All Gurus;



	I am trying to do the dynamic statement by adding the user defined WHERE

clause behind the SQL statement; my SQL statement is send over to a linked

server to process and result return to the caller. I I however encountered

some problem executing the SELECT statement as below:



	SCENARIO (1)

	============

		DECLARE @where_clause			VARCHAR(255),

        		  @clause				VARCHAR(255),

        		  @a1 				CHAR(6),

        		  @a2 				VARCHAR(65),

        		  @a3 				VARCHAR(65),

        		  @a4 				datetime



		SET @clause = "SELECT @a1 = code, @a2=name, @a3=name, @a4=dte_update

	      FROM linkSer.pub.dbo.street"



		SET @where_clause = " WHERE DATEDIFF(dd, dte_update , getdate()) > 0"

		SET @clause = @clause+@where_clause



		EXECUTE @clause  <<---- ERROR Could not find server

					'SELECT @a1 = st_code, @a2=st_name, @a3=pwdst_name, @a4=dte_update

  					 FROM linkSer' in sysservers. Execute sp_addlinkedserver to add

					 the server to sysservers.





	SCENARIO (2)

	=============



		DECLARE @where_clause			VARCHAR(255),

        		  @clause				VARCHAR(255),

        		  @a1 				CHAR(6),

        		  @a2 				VARCHAR(65),

        		  @a3 				VARCHAR(65),

        		  @a4 				datetime



		SET @clause = "SELECT @a1 = code, @a2=name, @a3=name, @a4=dte_update

	      FROM linkSer.pub.dbo.street"



		SET @where_clause = " WHERE DATEDIFF(dd, dte_update , getdate()) > 0"

		SET @clause = @clause+@where_clause



		GO <<--- The command(s) completed successfully.

		SELECT @a1 << --- NULL, in fact should have some value returned



Can gurus help me, how can I set the dynamic select statement and able to

return the value (@a1 in this case).



	Thanks.




  Return to Index