SQL Server select from non-existent table - sql-server

I have a query in classic asp where SQL statement is this:
Select * from active_Case
I verified in the DB connection it is using and found there is no such table / view. But a table does exist by the name of Cases. Internally it appears to be selecting from this table itself.
Actually this is somebody else's code. Thus I am not sure how is it possible. Is it really possible or am I missing something?

this will give you the base table name
select name, base_object_name
from sys.synonyms
where name = 'active_Case'

other than tables and view you can even check in User defined table type under types. or there might be chance your table is having a schema other than 'dbo.'

Related

Why can't SQL Server database name begin with a number if I run a query?

Recently I found an anomaly with SQL Server database creation. If I create with the sql query
create database 6033SomeDatabase;
It throws an error.
But with the Management Studio UI, I can manually create a database with a name of 6033SomeDatabase.
Is this expected behaviour or is it a bug? Please throw some light on this issue.
Try like this,
IF DB_ID('6033SomeDatabase') IS NULL
CREATE DATABASE [6033SomeDatabase]
I'll try to give you detailed answer.
SQL syntax imposes some restrictions to names of database, tables, and fields. F.e.:
SELECT * FROM SELECT, FROM WHERE SELECT.Id = FROM.SelectId
SQL parser wouldn't parse this query. You should rewrite it:
SELECT * FROM [SELECT], [FROM] WHERE [SELECT].Id = [FROM].SelectId
Another example:
SELECT * FROM T1 WHERE Code = 123e10
Is 123e10 the name of column in T1, or is it a numeric constant for 123×1010? Parser doesn't know.
Therefore, there are rules for naming. If you need some strange database or table name, you can use brackets to enclose it.

Where to find object in SQL Server and determine if it's a view or table?

I am having a problem that I think is easy to resolve but cant put my finger on it.
I did not create this database or jobs so troubleshooting my way through it.
I have a SQL Server job that is failing, it has multiple steps.
One of the steps is
select * into [Pastel_OrderStock] from [Pastel_SOProducts]
This fails with error:
Error converting data type varchar to float.
Now if I try and go:
select * from [Pastel_SOProducts]
it gives same error.
My issue is that I can't see a table or view Pastel_SOProducts in any of my database tables or views but it obviously exists given the error.
How can I find out where this is and more importantly view the table structure or view syntax?
Thanks in advance
It will be in sys.objects. The type and type_desc will show what object type is.
Things to check:
Check your permission. Your DB administrator could have restricted your account to certain tables.
Check the table's owner is not dbo.
As mentioned in another answer you want to pull the 'type_desc' from the sys.objects table:
select name, type, type_desc from sys.objects where name = 'Pastel_SOProducts'

MSSQL schema problem - I renamed a table , now the database is referencing the old name

here is my problem how to fix the schema of a renamed table manually.
I have a table called ActiveCustomers
I renamed it to ActiveCustomersOld
I then renamed it again ActiveCustomers ( to its original name)
Select * from ActiveCustomers it does work.
But if I rename the table to ActiveCustomerOld
select * from ActiveCustomers works.
Why? Because SQL schema it mapping it to ActiveCustomerOld table to the name ActiveCustomers.
How do I change the schema of a table to mapp to certain keywords.
For example
Select * from A
should map to a table called c
You could consider using a SYNONYM if you are using SQL Server 2005 or greater (it's always a good idea to specify the version you are using). You should also ALWAYS use the schema prefix when creating or referencing entities.
you can use synonyms to achieve that, though i don't know if you can mask actual entities that way.

Unable to carry out operations (create trigger, drop table) for a table I created

I am using a SQL Server database with SQL Server Management Studio where I have existing tables. I add a few tables to it and it works just fine. However, for subsequent operations such as
Drop table XXX --OR
Create Trigger YYY on XXX
I run into a error statement that reads:
i) Cannot drop table XXX as it does not exist or you do not have permissions
ii) The object 'XXX' does not exist or is invalid for this operation
I tried to carry out an Insert operation but that showed me a similar error (The object 'XXX' does not exist). I can see this maybe a permissions issue since I am using an existing database. However, in that case, I should have been unable to create a table as well?
Can anyone pinpoint how I can work myself around this and what the problem is?
What is your default schema?
SELECT name, default_schema_name
FROM sys.database_principals
WHERE type = 'S';
Try qualifying your references to the table as SchemaName.XXX and see if that helps.
Most of times when I had similar situations tables were created in system databases (master, tempdb..). Of course it was my mistake.
So maybe try to search for a tables in other databases?

How to merge table from access to SQL Express?

I have one table named "Staff" in access and also have this table(same name) in SQL 2008.
Both table have thousands of records. I want to merge records from the access table to sql table without affecting the existing records in sql. Normally, I just export using OCBC driver and that works fine if that table doesn't exist in sql server. Please advise. Thanks.
A simple append query from the local access table to the linked sql server table should work just fine in this case.
So, just drop in the first (from) table into the query builder. Then change the query type to append, and you are prompted for the append table name.
From that point on, just drop in the columns you want (do not drop in the PK column, as they need not be used nor transferred in this case).
You can also type in the sql directly in the query builder. Either way, you will wind up with something like:
INSERT INTO dbo_custsql
( ADMINID, Amount, Notes, Status )
SELECT ADMINID, Amount, Notes, Status
FROM custsql1;
This may help: http://www.red-gate.com/products/sql-development/sql-compare/
Or you could write a simple program to read from each data set and do the comparison, adding, updating, and deleting, etc.

Resources