473,382 Members | 1,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How do I Convert a SQL Script into a View or Function in order to Extract Data

Is it possible to convert the following script into a view. Ultimately i would like to draw data using excel.
The Script goes as follows:


Expand|Select|Wrap|Line Numbers
  1. DECLARE @columns VARCHAR(2000)
  2. SELECT
  3. @columns =
  4. COALESCE
  5. (
  6. @columns + ',[' + cast(DATEPART(yyyy, tr_date) as varchar) + cast(DATEPART(mm, tr_date) as varchar) + ']',
  7. '[' + cast(DATEPART(yyyy, tr_date) as varchar)+ cast(DATEPART(mm, tr_date) as varchar) + ']'
  8. )
  9. FROM
  10. audit
  11. GROUP BY 
  12. DATEPART(yyyy, tr_date),DATEPART(mm, tr_date)
  13. ORDER BY DATEPART(yyyy, tr_date),DATEPART(mm, tr_date)
  14.  
  15.  
  16.  
  17. DECLARE @query VARCHAR(8000)
  18. SET @query = 'SELECT *
  19. FROM
  20. (
  21. SELECT
  22. stock_code,
  23. cast(DATEPART(yyyy, tr_date) as varchar)+ cast(DATEPART(mm, tr_date) as varchar) as trdate,
  24. qty
  25. FROM
  26. audit
  27. ) PIV
  28. PIVOT
  29. (
  30. SUM(qty) FOR trdate in (' + @columns + ')
  31. ) AS chld'
  32. EXECUTE (@query)
  33. GO
for the sake of my question the output is not important but the result is.
Jan 31 '13 #1
2 1685
Rabbit
12,516 Expert Mod 8TB
You can't convert that to a view. Other than the data might come from Excel, I don't see what bearing it has on pivot tables.
Jan 31 '13 #2
ck9663
2,878 Expert 2GB
If you really need to, just create the view every time you need to run your query. Make it a dynamic create statement so you don't manually create it every time.

Happy Coding!!!


~~ CK
Jan 31 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Fran?ois Bourdages | last post by:
Hi is there a way to know if object (view, function, etc) are invalid ? let say a have a table t1 (field col1, col2) and a view v1 (field t1.col1, t1.col2) if I drop t1.col2, the view v1 is not...
1
by: Harry Zoroc | last post by:
As the subject said I am searching for a tool to extract data from SQL databases (e.g. MySQL) and convert it to an XML document. And vice versa a tool which enables me to insert a given XML data...
0
by: Ashok | last post by:
Hi, is it possible to extract data from a web based java applet in order to enter that data in mysql? for example, something that would let me extract the data shown in applet on...
1
by: Matik | last post by:
Hey, First, sorry if this post appear twice, because, I can not find my post recently send, trying to post it once again. I'm out of ideas, so, I thought, will search help here again :( I'm...
0
by: D Kon | last post by:
Hi, I have built a VC++.NET dll function in which you pass an array of double data and 2 additional input arguments and it returns a pointer of the data address and 2 additional scallar values. ...
2
by: missolsr | last post by:
hi, I am using jpcap to capture OLSR topology control (udp) packets. Does anyone know how to extract data (the way ethereal does it) from the olsr packet? There are methods to extract data...
1
by: bibie | last post by:
How to extract data from mssql and then convert it to mysql using VB6.0. How to connect the mssql..I know a little bit of VB6.0 but only create an interface using STANDARD EXE. Someone told me to...
2
by: tgmcnaughton | last post by:
I'm brand new to server-side scripting. I don't even know if javascript can do this. I would like a script running on my server to periodically login to and check my email account and each time...
6
by: Savita23 | last post by:
I am trying to pass a query string from the main form to subform to display records in datasheet view.I tried using the following code in the form_load() event to populate the subform(Child0) ...
1
by: Unregistered | last post by:
how to extract data from Pmw EntryField to usable variables to be passed to a script
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.