Creating a new table in Visual Studio 2013 - sql-server

project->add new item->data->service based database->(name of DB)->add
although the DB has been created,and database and its' table's folder are in the server explorer, it is not allowing to create a new table by right clicking on table folder.It's only giving refresh and properties as options. why is that?? what can i do to crate a new table?

To create the table:
In Server Explorer or Database Explorer, expand the Data Connections node, and then expand the SampleDatabase.mdf node.
If the explorer for your version of Visual Studio isn't open, choose View, Server Explorer or View, Other Windows, Database Explorer on the menu bar.
Open the shortcut menu for Tables, and then choose Add New Table.
The Table Designer opens and shows a grid with one default row, which represents a single column in the table that you're creating. By adding rows to the grid, you'll add columns in the table.
In the grid, add a row

Go to sql server studio>create new query window then write script like this to create a table
CREATE TABLE [schema].[sampletable](
[RecordNo] [int] NOT NULL,
[ID_NO] [nvarchar](50) NOT NULL,
[NAME] [nvarchar](200) NULL,
[Status] [varchar](20) NULL
)

Related

Access requires table relink of SQL Server table every time CurrentDB is opened

I have an Access database that uses SQL Server as its backend. One table is the data for a subform which is loaded into a parent form and displayed in the parent forma as a datasheet. The datasheet is intended to have rows added to it by users.
The subform backend table is defined in SQL Server with a primary key [ID]. The primary key is linked to the Master ID of the current record displayed in the parent form. The subform is then filtered by the relationship between the subform table primary key and the parent form master ID. Nothing unusual here.
The problem that when the parent form is first opened in Access the subform is cannot have records added. It is not until the linked table manager is opened and the backend table is RELINKED can the user add records to the subform. Oddly enough, when relinking the table I am always prompted for the primary key of the table even though it is defined in SQL Server.
The subform has the following settings
Data Entry: Yes
Allow Additions: Yes
Allow Deletions: Yes
Allow Edits: Yes
Allow Filters: Yes
Record Locks: No Locks
Here is the CREATE TABLE for the backend table:
CREATE TABLE [dbo].[ProjectHealth](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Project Health] [nvarchar](2000) NULL,
[Timestamp] [datetime] NULL,
[ProjectID] [int] NULL,
[Entered By] [nvarchar](255) NULL
) ON [PRIMARY]
GO
Again, the RELINK solves the problem. But a VBA TableDef.Refreshlink call does not do the trick. Even if I reset the primary key from VBA.
My suspicion is that I have modified some value in the subform settings that have caused this problem which started happening after a few days of normal behavior. Thanks in advance for any suggestions you can provide.

Remove Summation on Power BI SQL Server Column

How do I remove a summation property for a Power BI SQL table? I have a Customer Transaction Table. For some reason PowerBI is trying to Measure Sum the CustomerTransactionId primary key row. I do not want this added. Please see picture below.
I just want to display all rows in a table.
CREATE TABLE [dbo].[CustomerTransaction]
(
[Customertransactionid] [int] IDENTITY(1,1) primary key NOT NULL,
[Customerid] [int] NULL,
[Quantitybought] [int] NULL,
)
msdn
This is actually done in PowerBI under the modeling ribbon/tab.
When you click on a field (on the right where they are all listed under Fields is fine), the modeling ribbon changes to reflect all settings for that particular field.
Under Default Summarization select 'Do not summarize'.
PowerBI sets Default Summarizations regardless of the data source.

Can I get a table script with its data

I am using a SQL Server 2008 database.
I have two databases namely db1 and db2. In both there is a table tblcountry. I create this on 1st database. Then how can I script with its data for create on 2nd database?
I use the code below
CREATE TABLE [dbo].[tblCountry]
(
[record_Id] [int] IDENTITY(1,1) NOT NULL,
[country] [nvarchar](150) NULL,
[nationality] [nvarchar](150) NULL,
[lsdMdfdOn] [datetime] NULL,
[lstMdfdBy] [nvarchar](350) NULL,
[isDeleted] [bit] NULL,
[isEnabled] [bit] NULL,
)
Then what code will I use for the getting include the data?
No, you cannot view the data if you are using the create query.
If you want to see the data of the table on second database then you can use this query on your second database db2
select * from [db1].[dbo].[tblCountry]
But you can not view the data and create query at the same time.
Although it may seem very wierd solution but I guess what you can do is you can copy the create query on the query analyzer window and beneath that write the select query and execute it. (But I guess this is how most of the programmers do that)
If you are on the same server or have a linked server:
CREATE TABLE tblCountry
SET IDENTITY_INSERT tblCountry ON
INSERT INTO [database2].tblCountry
SELECT * FROM [database1].tblCountry
SET IDENTITY_INSERT tblCountry OFF
The most Easy way for this problem is for you to
Right Click on the Database on the Object Explorer
click Generate Scripts
on the introduction click Next
Select radio button on Script entire database and all database
objects or you can just select specific tables or stored
procedures by selecting the other radio button
On the Set Scripting Options click on Advanced Select the things you
want to script
Then on the Query just change the database name after the query USE
db2
Right click on database and click on tasks and export data
you can use export data option in sql server...it will give you data with table script

SQL Management Studio "Table -> Script CREATE" creates ALTERs

When I try to script a table CREATE from a database, it doesn't always script all the columns inside a single CREATE TABLE. Some of the columns will be added via ALTER TABLEs. Why is this? Is SQL server capturing the history of the schema modifications, and any columns added after the initial creation are scripted with ALTERS? The reason this matters is because I'm scripting tables to be used within a VS Database project and it can't handle the ALTER statements.
CREATE TABLE [dbo].[tblPackageType]
(
[PackageTypeId] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[PackageTypeDesc] [varchar](50) NOT NULL
) ON [PRIMARY]
ALTER TABLE [dbo].[tblPackageType] ADD [EDIcode] [varchar](10) NOT NULL
ALTER TABLE [dbo].[tblPackageType] ADD [EDI211] [varchar](10) NULL
Is SQL server capturing the history of the schema modifications, and any columns added after the initial creation are scripted with ALTERS?
Yes.
If you want to get around this, you can alter the script to be a create including all the needed columns, and after you run it the updated definition will be saved.

Make a varchar(50) column unique

I have a column (which represents an e-mail) in a SQL Server database with varchar(50) as data type and I would like to make it unique (do not allow the same two e-mail addresses). I cannot find a way to make such column unique in SQL Server Management Studio.
How to do that?
In T-SQL it would be
ALTER TABLE MyTable WITH CHECK
ADD CONSTRAINT UQ_MyTable_Email UNIQUE (EmailAddress)
Or as an explicit index
CREATE UNIQUE INDEX IXU_Email ON MyTable (EmailAddress)
Edit: I can't see how to create a constraint in the SSMS GUI: other answers show how to manage indexes. I do only use SQL though, never the GUI for this kind of work
In the Object Explorer under the table right-click the Indexes folder and choose New Index....
In the window that appears enter Index name:, tick the Unique checkbox and add your email field from the Add... button then click OK.
Try this:
ALTER TABLE [dbo].[TableName] ADD CONSTRAINT UNQ__TableName__ColumnName UNIQUE ([ColumnName])
From this:
http://msdn.microsoft.com/en-us/library/ms191166.aspx

Resources