same query from remote server and on server, different results - sql

I have a server called ERP-SERVER, and a server called SQLDEV-SERVER.
They both have a blob instance, but we never copy over the complete blob to the SQLDEV-SERVER as that would be too much data.
So when trying to access a file on our test server, it should first check if that file exists on the SQLDEV-SERVER, and if not check if the file exists on the ERP-SERVER. This is where it goes wrong. This piece of code (SQL) used to work but somewhere along the way it broke. I have narrowed it down to the inter database query just returning completely different results.
so for instance i run this query on the ERP-SERVER instance in SQL management studio:
SELECT count(*)
FROM [erp-server].[Extranet_Blob].[dbo].[FileStorBlob]
this returns 223221 results.
When i run the same query on the SQLDEV-SERVER instance in SQL management studio, it returns 313 results.
It points to the same server and same database, yet a completely different count, which is why it is also not returning the files from the live environment when it is not found on the dev environment.
Any pointers as to where this problem could be situated?

Look very carefully at your linked server definition. When you are running the query on SQLDEV-SERVER it is using the linked server definition of that name rather than necessarily the ERP_Server. Is it possible that someone has fiddled with the definition?

Related

SQL Server Management Studio requiring full table path

Yesterday, in SQL Server Management Studio (SSMS) (v17.6) I could write and execute:
select * from MyTable;
Today, I now must write:
Select * from myDbName.dbo.MyTable;
However, the old syntax will recognize that the table is there and pull back the headers, but no content. Also, the intellisense with the older syntax.
Yet, in my VB code, I can still use the initial statement shown above.
Why? And, how do I change it back?
You are likely connected to another database on the instance. This commonly happens when the default database for the login isn't set to something explicitly. It defaults the the master database. You'll need to change it in the drop down for intellisense to pick up your table names, and for you to not have to fully qualify the server.database.schema.table.
Also, in the bottom right of your query window, make sure the query window which you are executing your query from is actually connected to the correct database. Since you aren't getting results back, I'd bet that you are also not connected to the correct database when you run your query, but an older or developmental database (where the data is different).

For a Front end access user interface connected to a backend SQL server, do I create new queries for tables in the SQL server or Access frontend?

I am a new database intern working with a access front end and SQL server backend database. The database was custom made for the company. One of my assignments is to take scripts and apply them to make four new tables. I am aware that I need to make a new query for each new table but I don't know if I should make the query in SQL server management studio or the frontend access program. I have tried copying and pasting the given scripts into a new query in access but I get an error message "invalid SQL statement expected 'DELETE', 'INSERT'...". I decided to try to break done the program a little bit and tested the first line
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id =OBJECT_ID(N'[dbo] .[FK_tblInstrumentInterfaceLog_tlkpInstrument]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblInstrumentInterfaceLog]'))
but the same error message keeps popping up. I even tried just SELECT * FROM sys.foreign_keys, and I got the error message "could not find file...". I am very much a beginner and any guidance would be appreciated.Basically am I supposed to be applying these scripts the server SQL database or on the front end access program?
Are you using a pass-through query? i.e. not just a select query. Access needs to know where to send the query and since you are using TSQL not Access SQL this needs to be executed on the server.
Normally when you query a linked table the information of how to get the data (the connection string) is tied to the table. But for this kind of query you'll probably need to tell Access explicitly. Unless you are using an ADP/ADE, then the connection info travels with the program not the table.
As a general rule, you use SQL management studio (SSMS) to create and run those scripts. So the general accepted approach here is such scripts will not be placed in the front end. As noted such scripts if for some reason must be placed in the front end, then you have to create them as pass-though, but EVEN in this case you want to use the SSMS to create such quires.
So the answer here is you create the new scripts and make table queries in the back end, or in this case using the SQL server management studio.
The syntax checking, query editor etc. in recent versions of SSMS now has auto-complete etc. and you can test/write/update those scripts in SQL server. Once you have such a query or even several of them, then the resulting “several” statements can be pasted into a front end query that been created as pass-though. If you do not use a pass-though query, then you are creating and using and assuming client side SQL (JET (now called ACE)).
The client side has it own version of SQL syntax, and it is NOT 100% compatible with the SERVER SIDE. If you writing SQL in the client that is NOT pass though, then you using a linked table to SQL server. These linked tables thus will use local (JET/ACE) based SQL queries. The ODBC driver thus translates this SQL into server side compatible syntax. However the JET/ACE sql syntax is very limited when compared to SQL server and no server side commands exist in this SQL syntax for the client data engine (JET/ACE)
So for many quires, you will and can simply build such queries using the Access query builder.
However for SQL that needs to run 100% server side then such quires has to be setup as pass-though and are in most cased built + tested using SSMS.

SQL Server Select Query - Specifying server as a parameter

We have 2 servers - one production, one test/development. I wanted to run some SQL checks and updates against production data but write the changes to test/development server, so people wouldn't see the changes.
Using SQL Server Management studio, I ran the cursor with the checks and updates in it. I was actively connected to test/development. However, I wrote my queries as follows.
SELECT * FROM [Production_Server].[Production_DB].[schema].[table]
I was under the impression this would look at the production server, however, it did not. It looks at the test/development server. I have access/rights in both environments.
Is there something I overlooked permission wise to get this to work? Or is just how it is intended to work?
"[Production_Server]" must be the wrong linked server name.
Run the below sproc to find the correct value to use
exec sp_linkedservers

Sql: export database using TSQL

I have database connection to database DB1. The only thing I could do - execute any t-sql statements including using stored procedures. I want to export the specific table (or even the specific rows of specific table) to my local database. As you can read abve, DBs are on diffrent servers meaning no direct connection is possible. Therefore question: Is it possible to write query that returns the other query to execute on local server and get data? Also note, that table contains BLOBs. Thanks.
If you have SQL Server Management Studio, you can use the data import function on your local database to get the data. It works as long as you have Read/Select access on the tables you are trying to copy.
If you have Visual Studio you can use the database tools in there to move data between two servers as long as you can connect to both from your workstation.
Needs Ultimate or Premium though:
http://msdn.microsoft.com/en-us/library/dd193261.aspx
RedGate has some usefull tools too:
http://www.red-gate.com/products/sql-development/sql-compare/features
Maybe you should ask at https://dba.stackexchange.com/ instead.
If you can login to the remote db (where you can only issue t-sql), you may create linked server on your local server to the remote and use it later directly in queries, like:
select * from [LinkedServerName].[DatabaseName].[SchemaName].[TableName]

SQL Azure: sp_helptext gives non-runnable source code

When trying to automate reading out constraint information using sp_helpconstraint I got the bright idea of pulling out the source code of the built-in SP directly and run it myself (since it returns multiple result sets so those can't be stored in a temp table). So I ran exec sp_helptext 'sp_helpconstraint' (on SQL Azure) to generate the source code, and copied it into a new query window.
However, when I run the SP (on SQL Azure), I get lot's of error messages -- for example, that object syscomments doesn't exist even though I am using the exact same source that runs perfectly when calling sp_helpconstraint directly. Just to make sure it wasn't an anomaly with the procedure or a mistake in my copy/paste execution, I tested the exact same procedure on SQL Server 2008, and if I directly copy the SP source into a new query window, it runs perfectly (obviously after removing the return statements and manually setting the input parameters).
What gives?? Do built-in SP's run in a special context where more commands are available than normal on SQL Azure version? Is sp_helptext not returning the actual source that is being run on SQL Azure?
If you want me to try anything out, give a suggestion and I can try it on our SQL Azure Development instance. Thanks!