Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

build temp table using exec

Posted on 2013-01-03
2
Medium Priority
?
632 Views
Last Modified: 2013-01-07
apparently can't use parameter in an open query so have to use exec query

I am trying to create temp table using the results of the open query but am running into problems

here is what I have so far:

(for testing purposes I am hard coding user value. )




Declare @username varchar(50)
Set @username = 'dryan'

Declare @query varchar(500)



Set @query =

' WITH TempTableTierCreditsMonth1 (
      department, pager
      )
AS (

select department, pager
 from openquery
(AD1,' +  char(39) + 'SELECT  department, pager
FROM '  + char(39) + char(39) + 'LDAP://test.local'  + char(39) + char(39) +
 ' WHERE objectCategory =  '  + char(39) + char(39) + 'Person'  + char(39) + char(39) +
' AND objectClass = '  + char(39) + char(39) + 'user'  + char(39) + char(39) + ' and samaccountname =  '  + char(39) + char(39) +
@username  + char(39) + char(39) + char(39) +  ');'


exec(@query)
0
Comment
Question by:johnnyg123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
TempDBA earned 2000 total points
ID: 38740993
there is a little tweak here. You need to build your openquery thing as a dynamic. See the following link. Had the same issue before and it worked for me:-
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=59179
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38742375
You could also create a global temp table on the local server.  

First, insure the name is absolutely unique -- I use code like:: '##' + replace(cast(newid() AS varchar(60)), '-', '') :: for that.  [The only disadvantage is that forces all code that references the table to be dynamic SQL.]

Create or load that table using OPENQUERY.

Build index(es) on the table as needed.

Do the local queries that need that data.

Drop the global temp table.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question