creating view in sqlserver - sql-server

I trying to create view linking 2 tables admins and news
create view v_news as
SELECT [n_id]
,[n_title]
,[n_detail]
,[n_date]
,[n_sdate]
,[n_edate]
,[n_admin]
,[a_name]
,[a_email]
,[a_role]
,[a_status]
FROM hed2.dbo.hed_news,hed2.dbo.hed_admins
where hed_admins.a_id=hed_news.n_admin
This message is displayed:
Command(s) completed successfully.
but there is no view in VIEWS folder.
When I try to run the same query again then it says:
There is already an object named 'v_news' in the database.
I am connected with windows authentication
I tried reconnect and restart sql server but ....

Your view is using ANSI 92 syntax. While this will work for older database versions, it will not work in SQL Server 2012. See Mike Walsh's blog on this topic.
1 - When using SSMS views do not show up right away. Right click and hit refresh.
2 - It is very important to make sure you are in the correct database. I am sure many people, including me, have create an object or two in master. This is the default for a new login.
This can be changed by changing the default database for your login.
3 - Execute the USE command to change the database context (default).
The snippet below is a SQL Server 2012 compliant version.
USE [hed2]
GO
create view v_news as
SELECT [n_id]
,[n_title]
,[n_detail]
,[n_date]
,[n_sdate]
,[n_edate]
,[n_admin]
,[a_name]
,[a_email]
,[a_role]
,[a_status]
FROM dbo.hed_news JOIN dbo.hed_admins ON hed_news.n_admin = hed_admins.a_id
GO

Three things:
You must use JOINS and the way your query is written is bad. Just an observation. Nothing to do with your question.
Did you try running SELECT * FROM v_news? That would return results.
Right click and views in SSMS and select refresh. It will show up in the list
Raj

Related

DB Link from Oracle to MS Server

I am new to this forum and already searched for 45 minutes to find a solution for my problem. I hope you can help me.
A Gateway to a remote Microsoft SQL Database was installed on a Oracle Server (Oracle 12c). The tsnnames.ora file was appropiately set up.
For the connection, I created a database link (In the Oracle DB) as follows:
CREATE DATABASE LINK TEL CONNECT TO "fb_B2C" IDENTIFIED BY "passwort" USING 'dg1msql';
When I now execute the Select statement:
SELECT "name" FROM "sys"."databases"#TEL
it shows me the according databases. Among others, I can see the AB_Colors database.
Now, I want to select a view in the AB_Colors database.
Due to the fact I can connect to this database via Excel, I know that in the database AB_Colors, there are 10 Views(A,B,C,..). I would like to select the View C from the database AB_Colors via the DB LInk.
Owner of the View is b2b.
How do i need to formulate the select statement to do it?
I already tried different writings:
SELECT * FROM b2b.C#TEL;
SELECT * FROM "AB_colors"."b2b"."C"#TEL;
SELECT * FROM [AB_Colors].[b2b].[C]#TEL;
The common error message is: View/Table does not exist
I highly appreciate your help,
Fedja
This is the correct format
SELECT * FROM "b2b"."C"#TEL;
The issue maybe because the database you want to select from is not the one specified in the gateway for dg1msql. You can't add the database name to the query so you must specify it in the gateway connection.
This is defined in
$ORACLE_HOME/dg4msql/admin/initdg4msql.ora
where you should check HS_FDS_CONNECT_INFO

Advice on transfering data from one db to another, syntax

I'm fairly new to SQL Server. I have done basic admin, backups etc. I have also spent 2 years doing MySQL for a software company offering software support for their MySQL bespoke program. I'm mainly a tech guy (desktop, Networking) but getting my head round this DB stuff!
I have started with a company that run SQL Server 2005 and need some stuff doing, and I am struggling with the syntax more than anything. The company have 4 SQL Servers running the same db's (program wise) for 4 differing locations.
What I am trying to do is copy the updated cost price list from table 1 to the other tables with * criteria. Basically copy table.parts from server1.parts to server2.parts * currencyconvertion field * markup (%)
That bit seems to be quite easy except I cannot get the db's to link. I enter the server name which contains - and the syntax says wrong eg uk-server1 'can't find 'uk'? Also I am unsure in the 4 part address is correct servername, dbname, schema, table?
Right ok. Previously when tried I was unable to link the two servers. I have now resolved this and the server is now linked. I have been told that maybe there is a need for [] to quote'' server name. I have tried this with no success. The problem seems to be the name of the server having a - uk-efacs. as soon as I type this and remember it is now linked the herror is can't find server efacs an uk is wrong?? It's not ready the full server name? WHY?
Figured this out by trial and error just needs [] by server name ie [uk-efacs].db.table.field. This now is ok just need to work on my syntax as the query shows errors.
Try creating a Linked Server record on the server you're running this from. In Object Explorer (in SSMS) expand Server Objects, right click Linked Servers and select new. Select SQL Server and type the name of your remote server and then try your query again. Bit puzzled as the snippet you provided
update partmaster
set partmaster.fsunit = uk-efacs.efacsdb.partmaster.fsunit * uk-efacs.efacsdb.currency.currate * 1.32
Seems to parse just fine.

SQL Server Profiler showing EF queries against master database?

What am I missing here? The queries I see in SQL Server Profiler are all targeted against the master database, which makes it difficult to filter by database name ... which event or events should I be watching so I can filter by database name.
The bigger question, what exactly is going on here?
You should remove this 'MultipleActiveResultSets=True' from your EntityFramework connection
string
after that, you can see the target database name show in the Profiler , instead of master.
In my option, maybe ADO.NET team want to make use the MultipleActiveResultSets feature to get
data from DB, so they have to access master.
MultipleActiveResultSets is about raise one query and don't return all its result (like in foreach statement in LINQ) , and in the same time ,raise another query to get another data in the same session.
By default, this behavior is not allowed by DB. SO.....
I was able to get around this issue, including leaving MARS active by adding an application name to my connection string:
Data Source=database_server;Initial Catalog=MyDatabase;Trusted Connection=true;MultipleActiveResultSets=True;Application Name=MyDatabase;
Then you can filter on application name.
If this is for SQL Server 2008 R2, in your trace properties, on the 'Events Selection' tab, check 'Show all columns'. You should then be able to create a column filter based on DatabaseName.
I believe you'll have to pause or stop your trace to make these changes.
As K Ivanov pointed out, having MARS (MultipleActiveResultSets) enabled will show the DatabaseName as master in SQL Profiler. By setting this to false, it'll show the proper DatabaseName, but then you lose the ability to have MultipleActiveResults.
you can use LoginName or HostName to filter in the profiler
For some reason, if I select the SP:CacheHit event, it now shows the queries against the correct database and I am able to filter by it. What is that event exactly?

Find Which applications access my server using Profiler Trace with Application Name column

I Need to find out what are all the applications that use my sql server.
I'm using Profiler trace to do this (if there's another way to do this I would appreciate it)
On Profiler I'm using a Replay template, and after looking at the trace result I see that there's a column called Application Name, I'm wondering if there's a way to get the distinct ones (the trace is on a .trc file).
(By the way is this supposed to be posted on stackoverflow or serverfault?)
Thanks,
Gabriel
Try this:
SELECT DISTINCT ApplicationName
FROM ::fn_trace_gettable('C:\YourFolder\YourTraceFile.trc', DEFAULT) t
You can actually do this right from within Profiler in SQL Server 2008.
Create a trace with the following two events:
Security Audit : Audit Login
Security Audit : Existing Connection
For those two events, capture the following columns:
Event Class
Application Name
SPID (required)
Event Sub Class
Add a filter to Event Subclass to restrict it to values of 1. This filter will only capture non-pooled logins. This should give you all your existing connections and any new logins that occur during the time you are running your trace.
Next, in the organize columns, move Application Name up to the "Groups" section. This will now group all the results by the Application Name.
This is a pretty light weight trace and shouldn't put much (if any) load on the server if you restrict it to just those events and apply the filter.
(I'm pretty sure previous versions work the same way. I just don't have one in front of me to test.)

weird error using SQL-Server2005 SPROCs from MS Access 2000: ";1" in name --> not found

I have a weird problem here.
In short to the environment we have:
There is a (newly set up) Win2003 Server with a SQL Server 2005 Express database. I connect to it via a MS Access application.
Since I switched to the new server (restored a backup from the former server on it) all SPROCs (only in Access) have a ;1 after their name, hence cannot be found.
If I try to open a SPROC in Access (dbl click in overview), it asks for the parameter, then says cannot be found.
If I try to open, say, a report based on it, same result. If I change the name of the SPROC the report is based on to the name shown in the overview ( [sprocnam];1 ) it says "cannot be found" (of course, because the names did not change as one can see in Management Studio).
?!?
keep in mind that the Access-application worked fine with the database that I backed up on another server and restored to the newly set up server ...
Your help is greatly appreciated!
edit: I found a thread on SAP.com with someone experiencing the same problem, but without a solution: https://forums.sdn.sap.com/message.jspa?messageID=7947957
I can't tell why you have got this issue, but in In SQL Server you have the ability to create Numbered stored procedures. The procedures have the same name but may contain completely different code, look at this:
CREATE PROCEDURE [dbo].[spTest]
AS
BEGIN
SELECT ##MICROSOFTVERSION
END
GO
CREATE PROCEDURE [dbo].[spTest];2
AS
SELECT ##version
GO
EXEC spTest;1
EXEC spTest;2
I resolved the issue with an update of the clients office-installation to the latest service pack.
The one employee that notified me of the problem and me got new computers last week, and thus did not have the latest updates.

Resources