Solved

Save sql query result in sql server

Posted on 2016-11-10
15
48 Views
Last Modified: 2016-11-10
Hello,

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

Cheers
0
Comment
Question by:RIAS
[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
  • 5
  • 5
  • 5
15 Comments
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41882044
yes, there are ways ..
please provide specifics for your case
0
 

Author Comment

by:RIAS
ID: 41882047
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
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41882048
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41882052
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
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41882065
0
 

Author Comment

by:RIAS
ID: 41882067
Vitor,
Is there any way i can pass parameter to view ?

Cheers
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41882069
0
 
LVL 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41882074
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
 

Author Closing Comment

by:RIAS
ID: 41882083
Cheers!
0
 

Author Comment

by:RIAS
ID: 41882084
Thank you all for helping me!
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41882087
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
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41882092
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
 
LVL 43

Expert Comment

by:Eugene Z
ID: 41882133
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
 

Author Comment

by:RIAS
ID: 41882147
How to write a view query or how to create a view and have a query in it ?

Example:
Select ref from Table1
0
 
LVL 50

Expert Comment

by:Vitor Montalvão
ID: 41882169
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Lag Function DateDiff 2 26
Need help with part of sql query for a condition 12 32
SQL Query 20 19
Group ordersum by orderdate 3 13
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

734 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