Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic sql que

Status
Not open for further replies.

habesha

Programmer
Oct 30, 2006
68
US
I want my dynamic sql to return one result set on a variable

declare @string varchar(400), @result varchar(400), @columnName varchar(400)

set @string = 'select ' + @result + '= ' + @columnName + 'from ' + @tableName

exec(@string)

select @result

This query is not working, any suggestion?
 
Thank you,

here is onother question,
I want to transfer data into a table tmpTable from a table tmpTable2 which is found on another server

How can I do that
The database of tmpTable2 is db1
server is server1
owner of the table is dbo

Thanks
 
Write a normal Insert statement, but use the Server.Database.Owner.Table naming convention every time Table2 is mentioned in the query.



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
The server name is server1\serv
when I try to run this query

select * from server1\serv.databasename.dbo.tablename

it is complaining saying that

Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '\'.

what should I do
 
Put [] around the ServerName\InstanceName part of your code.



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
That is the way it is, I can't omit the slash

the server name id server1\serv
Thanks
 
Thank you

I am facing this error

Msg 18452, Level 14, State 1, Line 1
Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.

thanks
 
Is your login (or the login running the code) given permissions to access the database that holds table2? How big or granular are those permissions?

Have you added the linked server connection yet? (Sp_LinkedServers)



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
Yes I have sa permission on both servers and they are also linked servers

thanks
 
Run the sp_UpdateUserLogins on both DBs for that login. Maybe something is broken.



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
Is this undocumented proc 'sp_UpdateUserLogins'

I couldn't find any where.
How can I use it

Thanks
 
Sorry, I keep getting the name of that proc wrong. It's sp_change_users_login.



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top