• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 101
  • Last Modified:

Save sql query result in sql server

Hello,

Is there any way I can save a query and query results  in sql server.

Cheers
0
RIAS
Asked:
RIAS
  • 5
  • 5
  • 5
1 Solution
 
Eugene ZCommented:
yes, there are ways ..
please provide specifics for your case
0
 
RIASAuthor Commented:
Hi,

I have a query in acces which takes input from user.The user runs the query .  Similar thing in sql. Can view be done with parameter?
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can save to text files. Queries will be .sql scripts and Result will be .rpt files.
In SQL Server Management Studio (SSMS) you can find the save option in the menu File or just press the Save icon in the toolbar.
For save the Results you just need to send the results to a file (CTRL+SHIFT+F). You can also find the respective icon in the toolbar or in the Menu Query / Results to.
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
Vitor MontalvãoMSSQL Senior EngineerCommented:
I have a query in acces which takes input from user.
If you want something similar to MS Access queries then you don't have it in SQL Server. The more near you can have are the Views but then the user will need to run the View as a Select command:
SELECT *
FROM ViewName
WHERE Filter

Open in new window

So identically as they do for tables but using a View name instead of a table name.
0
 
Eugene ZCommented:
0
 
RIASAuthor Commented:
Vitor,
Is there any way i can pass parameter to view ?

Cheers
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Is there any way i can pass parameter to view ?
No. In SQL Server, parameters are used by Stored Procedures and Functions.
You can always have a SP that runs a View with the WHERE clause being build dynamically depending on the parameter. I think that's the best option you have.
0
 
RIASAuthor Commented:
Cheers!
0
 
RIASAuthor Commented:
Thank you all for helping me!
0
 
Eugene ZCommented:
it was not clear that you are asking about Sql server view

actually you can pass parameter in  view  by using Dynamic query in Sql server
 but case is closed....
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
actually you can pass parameter in  view  by using Dynamic query in Sql server
I don't think that's true. Can you give an example of passing a parameter to a View?

Dynamic query is what I suggested but inside a SP so parameters can be passed:
"You can always have a SP that runs a View with the WHERE clause being build dynamically depending on the parameter. "
1
 
Eugene ZCommented:
yes, it is correct directly you can not assign parameter to view in sql

however, it is possible to do it using as you said procs or as I said dynamic queries via
 proc non prod such as Ad-hoc from some front end or another mechanism can be used
-----
something like this


declare  @strparam varchar(800)= 'somevalue'
declare  @sql varchar(8000)

set @sql= 'select * from your view where paramColumn =' + @strparam
Exec (@sql)
1
 
RIASAuthor Commented:
How to write a view query or how to create a view and have a query in it ?

Example:
Select ref from Table1
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
How to write a view query or how to create a view and have a query in it ?
See a View as an virtual table so you'll need first to create it with the CREATE VIEW command. This way the View will be stored in the SQL Server database.
Now with the View created you can abstract from it and use it as you use any other table so you can easily perform a SELECT * FROM ViewName and you can also add the WHERE clause. Just check one of your dynamic SQL solutions that we've been provided to you.
1

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now