display information from sql server view [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
New to views with sql server, not sure how to view the information it has gathered with vbscript. I know it is not as simple as:
select * from EXAMPLE_VIEW
I want to get the information from my example view loop through it and display it, however when i try to do that sql query i get invalid object name.
My question is how do you sql query a view

You should probably be doing this:
SELECT column1, column2 FROM dbo.EXAMPLE_VIEW;
This is because if your user has a different default schema than dbo, it might be checking for some other view with the same name under a different schema. (You also know not to use SELECT *, right?)
So you should also make sure your user account has select permissions on the view and/or the table(s) behind it.
Bad habits to kick : using SELECT * / omitting the column list
Bad habits to kick : avoiding the schema prefix

select * from EXAMPLE_VIEW
...is the correct way to query a SQL Server view.
If it's not working for you, you have a different kind of problem: maybe no permissions, maybe no server connection, maybe a view called EXAMPLE_VIEW doesn't exist, maybe something else.
Whatever it is - we need the exact error message and as much example code as possible to help you.
But the SQL itself is correct.

Is your current database set correctly after establishing connection? It can be set using connnection string (http://www.connectionstrings.com/) or by specyfying it witin query itself:
SELECT * FROM MYDB.dbo.EXAMPLE_VIEW;

Related

Cannot view the SQL portion of a query in ACCESS?

I am currently working on a project of replacing our old access database queries, but on one of them I am not able to view the actual SQL View.
Does anyone know a way to force the view or to export it somehow?
Error causing problem:
The SQL statement could not be executed because it contains ambiguous outer joins.
Note that I can view the Design View without issue but when I right click on the tab and select SQL View is when I get the error.
I did attempt what #LeeMac mentioned below but same error occurs:
EDIT:
This question is not like Ambiguous Outer Joins?
The OP on that question can actually see and edit their SQL.
My issues is that I cannot see or edit the SQL as the SQL View wont open.
Try executing the following VBA code from the Immediate Window (accessible using Ctrl+G) in the VBA IDE (open the IDE using Alt+F11):
?CurrentDb.QueryDefs("YourQuery").SQL
Replace YourQuery with the name of your query.
This should print the SQL code which comprises your query - you can then analyse the SQL to determine the cause of the error.
It's odd this error would arise when merely viewing the SQL content of the query definition.
It makes me think that the query is perhaps referencing a crosstab subquery which is actually the cause of the error, but which needs to be evaluated in order for MS Access to determine the columns available when viewing the design of the query in question.
Try this:
hit ctrl-g, and from immediate window type in this:
saveastext acQuery,"Name of query","c:\test\mysql.txt"
Access ordinarily doesn't allow you to save invalid queries, so it's strange you somehow got into this situation in the first place.
If you can copy the query, you can easily get to the SQL by changing the query to a passthrough query, either through the GUI or through VBA:
Dim q As DAO.QueryDef
Set q = CurrentDb.QueryDefs!Query1
q.Connect = "ODBC;"
Debug.Print q.SQL
Passthrough queries are not validated, so you can freely read and write anything you want as SQL in it.
Note that this is irreversible when done through VBA. You can only change it back to a normal query once you made the SQL valid again. If you do it through the GUI, you can just not save it, though.
I had this problem and the issue was that i had a subquery that calculated fields but did not actually have a table in it. for example it would calculate first and last day of last month which is 2 calculated fields, then it was the first query in a series of queries that were built off it and the last one wouldnt resolve sql as original poster indicated also gave the ambiguous join message as well as query needs input table (which was that first subquery). i put a table with 1 record in it but didnt use the record and it worked.... so it just a needs a table in it.

Why some rows can be inserted into and some cannot? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
When I use the Cashier table or some other tables that have small amounts of records the process proceeds and table is inserted into the external database. But when I change the cashier into the transaction database (400k+ records), Visual Studio reports an error near "Transaction" Help would be appreciated thanks.
Cashier Database (working)
Dim query As String = "select * into MyDatabase2.dbo.Cashier from bos_primary_db.dbo.Cashier"
Transaction Database (not working)
Dim query As String = "select * into MyDatabase2.dbo.Transaction from bos_primary_db.dbo.Transaction"
This is the error message:
Incorrect syntax near the keyword 'Transaction'
this is probably because Transaction is a reserved word in SQL.
Depending on your RDBMS (that you didn't specify), there are ways to "escape" it:
for Sql Server, you should wrap reserved words in square brackets:
select * into MyDatabase2.dbo.[Transaction] from bos_primary_db.dbo.[Transaction]
For MySql you should use an apostrophe:
select * into MyDatabase2.dbo.`Transaction` from bos_primary_db.dbo.`Transaction`
For Oracle you should use double quotes:
select * into MyDatabase2.dbo."Transaction" from bos_primary_db.dbo."Transaction"
Note: You should always try to avoid using reserved words. This link describes my favorite way of do it.

SQL Import / Export Data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've got a Windows Form App linked to a Microsoft SQL Server database with loads of tables.
I've got 2 buttons over there (Import and Export) and when clicking on EXPORT, this should trigger a Stored Procedure that will generate a string to export the data only (loads of INSERT statements - not the table definition). The string generated by this will be saved as a script file. This will be later used to IMPORT that data on to a different database.
I was thinking of creating a stored procedure to do this (but I'm not entirely sure how) or if there is a better approach for this, I'm open to any suggestions.
Use Management Studio to generate the script.
Right-click on the database in Object explorer, from context menu choose "tasks", then "generate scripts".
In the dialog, select the tables you want the data scripted out from. On the next page set the destination, and click on the "advanced" button. There find the "type of data to script" option - choose "data only".
[edit] As per the edited question, you can automate this using Microsoft.SqlServer.Management.Smo.Scripter, the same lib the SSMS uses.
If you want to generate a script that will generate an insert statement for you, you could build it with a query, like this...
SELECT
'SELECT ' + CAST(ID AS VARCHAR(10)) + ',''' +
VarcharField1 + ''',''' +
VarcharField2 + ''' UNION ALL'
FROM Example
If you display the results as text (Ctrl+T), you can easily grab it for further manipulation. It will generate something like this...
SELECT 1,'Row1Field1','Row1Field2' UNION ALL
SELECT 2,'Row2Field1','Row2Field2' UNION ALL
SELECT 3,'Row3Field1','Row3Field2' UNION ALL
SELECT 4,'Row4Field1','Row4Field2' UNION ALL
You would need to put an INSERT [TableName] before the results and trim the last UNION ALL, but this works. I've used it several times before. SQL Fiddle example

Error message from simple SELECT statement with practice database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use the AdventureWorks practice database attached to SQL Server 2012.
A simple statement like:
SELECT * FROM HumanResources.Shift;
gives me this error:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'AdventureWorks2012'.
I also tried:
USE master
GO
SELECT * FROM HumanResources.Shift;
Received same error message. Any idea to why this is happening?
Is AdventureWorks2012 the name of a database or a table?
USE <database name>
means use the specified database to run the query against.
SELECT * FROM <table name>
means select all the rows from the specified table.
--Edit because of change in question--
You don't want to use Master, you want to use the name of your DB. Next, using the object explorer, I would make sure the table actually exists in the Database you created.

SQL Server Replace Command with WIldcard [duplicate]

This question already has answers here:
Perform regex (replace) in an SQL query
(5 answers)
Closed 5 years ago.
I am in need of some help. Thanks to fellow user davids, I was able to get certain things working within SQL Server and am in need of some more help. Here is what I am trying to do:
UPDATE Table1 SET keyfield=(REPLACE(REPLACE(Column1,'http://*/folder/',''),'.avi',''))
UPDATE Table2 SET keyfield=(REPLACE(REPLACE(Column2,'http://server2/folder/',''),'.mpg',''))
Can anyone help me out or point me in the right direction to get the wildcard to work? This particular column, the * is an IP Address and it will change all the time. Granted I will know the IP's ahead of time and can probably do it one by one, but I would prefer to have it automatically replace. Thanks in advance!
You should look into regular expressions.
My google karma found this article in MSDN Magazine.
Edit:
See also:
Perform regex (replace) in an SQL query