Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamic View?

Posted on 2005-04-25
6
Medium Priority
?
3,293 Views
Last Modified: 2011-09-20
Hello Experts,

I am building an application that uses SQL Server. Soon this will change to Oracle but that is not my biggest concern. My main concern is about a view.

This view has the following select statement:
Select t$hfdl, t$date, t$wrko, t$beur from ttirbh016700 where t$uitv = 1 and t$afgm = 1 and t$odat <> 0 and t$dctb = 0

The table this is selected from, should be different depending on a value my Visual Basic application reads from an INI file

the '700' part should be dynamic.

I am not the most frequent user of SQL Server or Oracle, but as far as I know, views are static. Anyone that knows a way to pass my INI value to the view? Or am I right and is this simply not possible. If it isn't...what is the way to go? Stored Procedures? Please someone advice..I need this as soon as humanly possible..

Regards,
Max.
0
Comment
Question by:DreamMaster
  • 3
  • 2
6 Comments
 
LVL 10

Assisted Solution

by:imrancs
imrancs earned 1000 total points
ID: 13858341
Use dynamic sql to create the view, like

CREATE PROCEDURE uspCreateView
   @TableName  Varchar(100)
AS

--this will create view dynamically with the of table that you will pass to the storeed procedure

EXEC ('CREATE VIEW MyView AS Select t$hfdl, t$date, t$wrko, t$beur from ' + @TableName  + where t$uitv = 1 and t$afgm = 1 and t$odat <> 0 and t$dctb = 0')


GO



--You can use this stored procedure as following

EXEC uspCreateView 'ttirbh016701'


Imran
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 13858408
Thanks for the quick reply Imran,

The view already exists on the server. I guess it can be altered in the same way...

If I would run this from Visual Basic, where I just run:

com_R5_cmd.CommandText = "SELECT * FROM R5_ONTVANGST"
rst_Triton_Project.Open cmd_R5_cmd.adOpenStatic, adLockBatchOptmistic

I'd have to change that to a stored procedure I call. This stored procedure could then call the information from my INI file and possibly alter my view to the correct one.

I am strongly thinking of changing the views to one stored procedure that has both table name and fields as parameters. Let me try out some options and I will get back to this asap...

Regards,
Max.
0
 
LVL 28

Accepted Solution

by:
rafrancisco earned 1000 total points
ID: 13858525
What you are trying to do is better done using a stored procedure.  Here's how your stored procedure will look like:

CREATE PROCEDURE GetDate @TableSuffix VARCHAR(3)
AS
DECLARE @SQL VARCHAR(2000)

SET @SQL = 'Select t$hfdl, t$date, t$wrko, t$beur from ttirbh016' + @TableSuffix + ' where t$uitv = 1 and t$afgm = 1 and t$odat <> 0 and t$dctb = 0'
EXEC (@SQL)

Hope this helps.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:DreamMaster
ID: 13858555
That is what I thought rafrancisco. Sadly I cannot test it at this very moment as I do not have the clients database. I will look over the answers given and either split or accept one accordingly...

Thanks you both.. :)

Regards,
Max.
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 13875418
I am going to split points between the two of you...both comments have been helpfull to me...

Regards,
Max.
0
 
LVL 10

Expert Comment

by:imrancs
ID: 13927998
Glade if I could help.


Imran
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

580 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