Invalid object name in SSMS for table in SQL Server 2016 - ssms

I created a table using an EF Core 2.1 migration and was able to populate it using EF in C#. The table shows in SSMS Object Explorer and I can execute queries against the table using "Edit top 200 rows" and "Select top 1000 rows". I tried an ad-hoc query:
use ArielOpnsDB
go
select * from ZipCodeCities
The table, ZipCodeCities, is show with the error, "Invalid object name 'ZipCodeCities'. Queries against all other tables and it shows in the creation scripts generated in SSMS.
What could be the problem and how can it be fixed?
Thank you.

Related

Table missing in a database but query works

I have a table TBLT_GLDET in my SQL Server 2008 database. When I open a new query window and select the right database and execute following query
select *
from TBLT_GLDET;
the data appears. But in the tables list the table does not appear.
Also when I try to open the table for a Crystal Report using database expert in Visual Studio, it says the table is missing in the database.
What is the reason for this?
I did following things. I double checked whether it's the right database.
I refreshed the database.

How to create a new table from existing table with both keys and data in SQL Server 2012 (Management Studio)?

How can I create a new table from an existing one in SQL Server 2012 , with both its keys and its data ?
This :
select * into New_Table
from Old_Table
Does not create the dependencies .
And this :
In Sql Server Managment Studio, right-click your existing table and
select Script Table as > Create to > New Query Editor Window
Doesn't get the data .
Thanks
The following instructions work well if you want a GUI based solution and are dealing with a moderate amount of data.
In SSMS, right-click on the database that contains the table and select Generate Scripts
Select the specific table(s) to recreate
Select the Save to new query window radio button and click Advanced to set scripting options for the table
Set the Types of data to script property to Schema and data
Modify the table name in the resulting script as appropriate
One way is to use something like SQL Server Data Tools (SSDT). This is a free, Microsoft, add-on to Visual Studio 2012/2013 (which you don't need to own). This tool will do schema and data compares. So all dependencies and schema objects will be created for you, in the correct order.

Entity framework returning different result than SSMS

I've encountered quite a strange problem
I'm trying to run a stored procedure via entity framework to work out the counts of various people with a date of birth within a certain range
I'm running the following code
select count(*), groupId from app_people p
where p.DOB >= '2012-10-02'
and p.DOB <= '2013-10-01
group by groupId
The issue I'm getting is that the same stored procedure executed via ssms returns 70 whereas entity framework returns 59
I'm inserting the result of the above query into a temp table before updating an existing table with the results.
Any suggestions as to why this would be happening?
Both are being passed the same parameters.
Using the following code to execute the proc via Entity framework, the procedure has been imported from the database and is included in the datamodel.
DatabaseContext.TestProcedure(false);
This could be a permissions issue as HLGEM pointed out. If the connection to SQL Server in code uses a different login than your SQL Server Management Studio SSMS login (e.g. windows credentials) you may see different results.
You can use the following TSQL to execute a query from another security context within SSMS. You could do this to compare the results of the same query for different users.
-- Execute as another user: https://learn.microsoft.com/en-us/sql/t-sql/statements/execute-as-transact-sql
EXECUTE AS USER = 'testuser';
-- TSQL with inconsistent results between code and ssms.
SELECT COUNT(*), groupId FROM app_people;

Invalid object name LocalDb Table

I'm trying to run a simple SQL query on a LocalDb database in Visual Studio 2013.
Here's the query in a file GrabWords.sql:
SELECT * FROM Words
where Words is a table in the NextGen.mdf database. When I run this, I get the error:
Invalid object name 'Words'.
For more information, I actually have two databases in the project. In the Solution explorer one is called NextGen.mdf, and the other Cards.mdf. In server explorer, the first (that I am trying to query) is called AutoGenEntities and the other is CardsDBContext.
I just want to run this query to test queries and view the database - is there another tool I should be using to do this?
Try this:
USE [your Database name]
SELECT * FROM Words
This should ensure that you are targeting the correct database.

sql table view problem

I am new to SQL and I am facing problems. When I create any table, I can't view it. Also, is it possible to enter the data into the table via management studio?
Like Harry 1234 xyz?
If you entered the query right, and it shows that the commands have been successfully executed, you should be able to see the created table with no entries by using select * from table_name.
If you still cannot see the data, try refreshing the database. Now you should be able to see your data.
Many database engines support entering data directly without SQL statements (like Harry 1234 xyz) like Microsoft SQL Server, MySql, etc. How you do it will depend on which database engine you're using.
You have to refresh "Object Explorer" in Management Studio.
It doesn't know that you created an object via SQL script