I created a table using the create table command but it does not show up in the object explorer. I have tried refreshing both the explorer and IntelliSense but it still does not show up. The table is present in the database I confirmed that by printing it out using the select command.
It appears like you created your table in the system master database instead of in your user defined database. There are a few things you can do:
At the top of your query, write USE your_database_name That will set your connection to run your query against a specific database.
In SSMS, on the top left by default I think, there should be a white box with a dropdown arrow next to it. This is a list of all the databases in the instance you are currently connected to. Click yours, and run your table create.
Fully qualify your table name during the create as my_database_name.schema_here.table_name_here
Always look at the bottom of your SSMS window and confirm you are running queries in the correct instance (dev vs prod) and running them against the correct database.
USE database_name
GO
CREATE TABLE dbo.my_new_table (ID INT)
GO
or
CREATE TABLE database1.dbo.test_user_table (ID INT)
GO
Related
I'm not sure how I ended up with this situation: I seem to have two SQL Server instances with the same name, in one machine.
Screenshot here
When I connect through SQL Management Studio I have access to the database of my application, but it is empty, tables have no rows.
However when my application connects through the next connection string it has all the rows it had inserted, I mean, the application works fine.
Data Source=DESKTOP-D5BH4BP;Initial Catalog=AppDB;User ID=AppUsr;Password=pass;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
So I guess each instance points to its own files.
The question is: How can I get rid of the empty instance??
You cannot have two instances with the same name on SQL Server, it is not possible at all.
The drop down you have shared in your screenshot is only showing the server names you have connected to in past using SSMS, you can type anything there but whether that server exists or not is something different.
To get rid of these names from the drop down list, just select the name and press Delete key.
Click on < Browse for more...> and you'll see the single instance
If you want to remove the duplicate entry from the drop down, just mouse hover on the entry and hit delete on the keyboard.
I'm getting this error when trying to deploy a SQL Server Data Tools (SSDT) database project:
View or function 'dbo.Employees' has more column names specified than columns defined.
I deleted a column from the underlying table and the view looks like this:
CREATE VIEW [dbo].[Employees] AS SELECT * FROM [$(ExternalDB)].[dbo].[Employees];
Doing a Google search brings back this page which says that SQL Server keeps meta data on views which must be refreshed. They mention this command:
EXEC sp_refreshview 'Employees';
If I put that in the pre-deployment script, it'll run before the column was dropped. If I put it in the post-deployment script, the deployment will throw the error before it gets executed. So my question is where or how can I do that with an SSDT project?
This is interesting as by default ssdt will refresh any views which depend on any table that has changed as part of a deployment.
Was the column dropped as part of a normal ssdt deployment?
In your publish profile or publish options are you setting ScriptRefreshModule to false?
Aside from this select * in a view is bad practice, put the full column list and this problem disappears, you can even right click the "select *" and choose to expand to get the full column list - do that instead :)
Ed
While trying to delete a SQL database from SSMS, I am not thrown any error but the progress bar just says executing for about 20 minutes now.
I set the database to Single User mode, tried taking it offline, no active SPID's , DBCC opentran() shows no active/open transactions.. yet still the same result when I try to delete it.
Referred to few posts from users with similar problem .. no luck yet.
Any suggestions much appreciated.
Instead of deleting by right clicking, used the below query to drop the database. Works now!
USE Master;
GO
DROP DATABASE [MyDB]
GO
Note: I could be totally off the mark here but per my understanding the DELETE option just issues a DROP command!
If you are trying to delete a system database then you cant, 2 options below to delete user defined databases:
1.Using SQL Server Management Studio
- In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
- Expand Databases, right-click the database to delete, and then click Delete.
- Confirm the correct database is selected, and then click OK.
Using T-SQL
-Connect to the Database Engine.
-From the Standard bar, click New Query.
-Copy and paste the following example into the query window and click Execute.
USE master ;
GO
DROP DATABASE DatabaseName ;
GO
Incase you wish to drop multiple databases:
USE master ;
GO
DROP DATABASE DatabaseName1, DatabaseName2 ;
GO
USE Master;
DROP DATABASE IF EXISTS [myDatabase]
To add a new column to the table I double click on the table's name in the Server Explorer in Visual Studio 2010 and then right click to insert a new column. But the problem is that the new column is gone after I have run my aplication with a connection with the database.
All new data added to the database is gone after I shut down Visual Studio. But the data is in the database while I use the application, because I can load and see the new data I just added. The only data that are left are the data I added while I created the database and table in Visual Studio.
I'm using a mdf file. I have checked if the mdf file is in Bin/Debug. One thing that is strange is that there a two .mdf databases in the Server Explorer .mdf and .mdf1. It's the last one I have been trying to alter.
What could be wrong? A helping hand is appreciated! I need to hand in this task as soon as possible to my teacher. Thanks!
Try using T-sql:
Alter table table1 add col1 datatype.
Then try to insert data into it:
INSERT INTO table1(col1,col2...)
VALUES(v1,v2...)
Then you try to test.
I've got a project where I'm attempting to use SQLite via System.Data.SQLite. In my attempts to keep the database under version-control, I went ahead and created a Database Project in my VS2008. Sounds fine, right?
I created my first table create script and tried to run it using right-click->Run on the script and I get this error message:
This operation is not supported for the provider or data source you are using.
Does anyone know if there's an automatic way to use scripts that are part of database project against SQLite databases referenced by the databases, using the provider supplied by the System.Data.SQLite install?
I've tried every variation I can think of in an attempt to get the script to run using the default Run or Run On... commands. Here's the script in it's most verbose and probably incorrect form:
USE Characters
GO
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'Skills')
BEGIN
DROP Table Skills
END
GO
CREATE TABLE Skills
(
SkillID INTEGER PRIMARY KEY AUTOINCREMENT,
SkillName TEXT,
Description TEXT
)
GO
Please note, this is my first attempt at using a Database, and also the first time I've ever touched SQLite. In my attempts to get it to run, I've stripped any and everything out except for the CREATE TABLE command.
UPDATE: Ok, so as Robert Harvey points out below, this looks like an SQL Server stored procedure. I went into the Server Explorer and used my connection (from the Database project) to get do what he suggested regarding creating a table. I can generate SQL from to create the table and it comes out like thus:
CREATE TABLE [Skills] (
[SkillID] integer PRIMARY KEY NOT NULL,
[SkillName] text NOT NULL,
[Description] text NOT NULL
);
I can easily copy this and add it to the project (or add it to another project that handles the rest of my data-access), but is there anyway to automate this on build? I suppose, since SQLite is a single-file in this case that I could also keep the built database under version-control as well.
Thoughts? Best practices for this instance?
UPDATE: I'm thinking that, since I plan on using Fluent NHibernate, I may just use it's auto-persistence model to keep my database up-to-snuff and effectively in source control. Thoughts? Pitfalls? I think I'll have to keep initial population inserts in source-control separately, but it should work.
I built my database using an SQLite SQL script and then fed that into the sqlite3.exe console program like this.
c:\sqlite3.exe mydatabase.db < FileContainingSQLiteSQLCommands
John
Well, your script looks like a SQL Server stored procedure. SQLite most likely doesn't support this, because
It doesn't support stored procedures, and
It doesn't understand SQL Server T-SQL
SQL is actually a pseudo-standard. It differs between vendors and sometimes even between different versions of a product within the same vendor.
That said, I don't see any reason why you can't run any (SQLite compatible) SQL statement against the SQLite database by opening up connection and command objects, just like you would with SQL Server.
Since, however, you are new to databases and SQLite, here is how you should start. I assume you already have SQLite installed
Create a new Windows Application in Visual Studio 2008. The database application will be of no use to you.
Open the Server Explorer by pulling down the View menu and selecting Server Explorer.
Create a new connection by right-clicking on the Data Connections node in Server Explorer and clicking on Add New Connection...
Click the Change button
Select the SQLite provider
Give your database a file name.
Click OK.
A new Data Connection should appear in the Server Explorer. You can create your first table by right-clicking on the Tables node and selecting Add New Table.