 |
|
ผมอยากรู้วิธี Generate Create table MS SQL (Script) กรณีที่ไม่มี Table ที่เราเขียนขึ้นมาใหม่ ให้มัน Generate table |
|
 |
|
|
 |
 |
|
ใช้คำสั่งนี้
Code (SQL)
SELECT name
FROM dbo.sysobjects
WHERE xtype = 'U'
จะได้ชื่อ Table ทั้งหมดที่อยู่ใน Database ที่เรา Connect อยู่มา ก็เอามาเช็คดูว่าขาด Table ไหน
อันไหนขาดก็ใช้คำสั่งนี้สร้างเอา
https://www.thaicreate.com/tutorial/sql-create-table.html
|
 |
 |
 |
 |
Date :
2012-06-22 08:25:15 |
By :
Nameless |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ linq จบเลย
กำหนด connection
สร้าง data context ด้วย connection ที่กำหนด
สร้าง table class
เอา table class ที่สร้างไปเป็น property ของ data context
จากนั้นก็ไม่ต้องทำอะไร จบเลย มันไม่เจออะไรมันก็สร้างให้เอง
ตั้งแต่ database, table
|
 |
 |
 |
 |
Date :
2012-06-22 08:48:41 |
By :
อิอิ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใน SQL Server น่าจะไม่มี CREATE TABLE IF NOT EXISTS `tb_name`
เช่น
CREATE TABLE IF NOT EXISTS [dbo].[customer](
[CustomerID] [varchar](4) NOT NULL,
[Name] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[CountryCode] [varchar](2) NULL,
[Budget] [float] NULL,
[Used] [float] NULL
)
แต่น่าจะเขียน function ได้ครับ เดียวลองดู
|
 |
 |
 |
 |
Date :
2012-06-22 09:12:26 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปกติผมทำเสร็จจะ ใช้ MS SQL ทำเป็นแบบสคลิปเอาตารางที่สร้างมาใหม่ ไปรันสคลิปบนเครื่องที่ไม่มี
หัวหน้าเลยบอกว่าคนเก่าเคยเขียนแบบ ให้มันเจ้นได้ลองหาวิธีดู
พอมีตัวอย่างฟังก์ชั่นบ้างป่ะ ผมโง่กว่าพวกพี่คิด
|
 |
 |
 |
 |
Date :
2012-06-22 09:21:01 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ
CREATE TABLE IF NOT EXISTS
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='customer' AND xtype='U')
CREATE TABLE [dbo].[customer](
[CustomerID] [varchar](4) NOT NULL,
[Name] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[CountryCode] [varchar](2) NULL,
[Budget] [float] NULL,
[Used] [float] NULL
)
GO
|
 |
 |
 |
 |
Date :
2012-06-22 09:25:48 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ขอถามหน่อยดิ สงสัย 2 บรรทัด
FROM sysobjects คืออะไร
xtype='U' คืออะไร
|
ประวัติการแก้ไข 2012-06-22 09:40:26
 |
 |
 |
 |
Date :
2012-06-22 09:37:40 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ดูตามรูปครับ
|
 |
 |
 |
 |
Date :
2012-06-22 09:45:57 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เขียนไว้นานแล้ว จากกระทู้เก่าๆ https://www.thaicreate.com/dotnet/forum/044317.html
Code (C#)
private bool TableExist(string TableName)
{
string sqlCommandString = string.Format("If Object_ID('{0}', 'U') Is Not Null Select 'true' Else Select 'false'", TableName);
SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
sqlConnection.Open();
object Result = sqlCommand.ExecuteScalar();
sqlConnection.Close();
return Convert.ToBoolean(Result);
}
|
 |
 |
 |
 |
Date :
2012-06-22 09:57:21 |
By :
อิอิ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เวลาเรียกใช้
Code (C#)
if (!TableExist("PokedexTable"))
{
CreateTable("PokedexTable");
}
Code (C#)
private bool CreateTable(string TableName)
{
string sqlCommandString = string.Format("Create Table [{0}] ([PokemonID] int Identity(1,1) Primary Key Clustered, [PokemonCode] nvarchar(50), [PokemonName] nvarchar(50), [PokemonHp] int, [PokemonAttack] int, [PokemonDefense] int, [PokemonSpAttack] int, [PokemonSpDef] int, [PokemonSpeed] int, [PokemonIcon] varbinary(MAX), [PokemonArtwork] varbinary(MAX))", TableName);
SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
sqlConnection.Open();
int Result = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
return (Result > 0) ? true : false;
}
|
 |
 |
 |
 |
Date :
2012-06-22 10:02:47 |
By :
อิอิ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ผมสงสัยอีกนิด1111111111111
คือ
คำถามที่ 1 ถ้าจะเช็คว่าใน table มีคอลัมน์ตัวที่ส้างขึ้นมาใหม่ หรือไม่ ถ้าไม่มีให้สร้างคอลัมน์นั้นขึ้นมา
เช่น เช็คตาราง customer ว่า มี คอลัมน์ Tel หรือไม่ ถ้าไม่มี ให้สร้างคอลัมน์ Tel ขึ้นมาใหม่
คำถามที่ 2 จะเช็คว่า มี Database ตัวที่เราสร้างขึ้นมาใหม่หรือไม่ ถ้าไม่มีสร้าง Database ขึ้นมาใหม่
เช่น ผมสร้าง Database ชื่อ Mydatabase ถ้าไม่มีให้สร้าง Mydatabase
|
 |
 |
 |
 |
Date :
2012-06-22 22:32:10 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เรื่องเพิ่ม Column ฟิวด์น่ะทำได้ครับ (เดียวพรุ่งนี้มาเขียนให้) ส่วนเพิ่ม Database น่าจะมีอยู่แล้วน่ะครับ
|
 |
 |
 |
 |
Date :
2012-06-22 22:47:09 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ตรวจสอบ Database ก่อนการสร้าง
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name='Sales')
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'D:\Database\saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Sales_log',
FILENAME = 'D:\Database\salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
|
 |
 |
 |
 |
Date :
2012-06-23 06:39:45 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ ALTER ADD Column
IF NOT EXISTS (SELECT * FROM sys.columns a
INNER JOIN sysobjects b
ON a.object_id = b.id
AND a.name='Used'
AND b.xtype='U'
AND b.name = 'customer')
ALTER TABLE customer ADD Used float NULL;
GO
ตรวจสอบใน Table ชื่อว่า customer มี Used อยู่หรือไม่ ถ้าไม่มีให้ ALTER ADD COLUMN
|
 |
 |
 |
 |
Date :
2012-06-23 06:55:05 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่วินครับ กระทู้ที่ 14 ผมลองแล้วไม่ผ่าน
มัน Error ว่าพาทไม่ถูกต้อง
Code (C#)
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name='Sales')
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Sales_log',
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
Code
Msg 5133, Level 16, State 1, Line 3
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\saledat.mdf" failed with the operating system error 3(error not found).
Msg 1802, Level 16, State 1, Line 3
CREATE DATABASE failed. Some file names listed could not be created. Check related errors
|
ประวัติการแก้ไข 2012-06-23 09:31:00 2012-06-23 09:31:30
 |
 |
 |
 |
Date :
2012-06-23 09:29:02 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแยกสร้างในโฟเดอร์ง่าย ๆ ดูครับ
|
 |
 |
 |
 |
Date :
2012-06-23 09:48:40 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวที่เขียนให้ผมลองแล้วผ่านทุกตัวครับ
|
 |
 |
 |
 |
Date :
2012-06-23 09:49:15 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่อได้แล้วครับ ตะกี้ผมดูไม่ดีเอง เชื่อมเป็น sever ไม่เลือกตัวมันเอง
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2012-06-23 10:39:04 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2012-06-23 10:46:29 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แทรกคำสั่ง USE [Database-Name] ก่อนด้วยทุกครั้งครับ
|
 |
 |
 |
 |
Date :
2012-06-24 17:21:00 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แทรกตรงไหน ยังไงเหรอครับพี่
|
 |
 |
 |
 |
Date :
2012-06-25 09:18:47 |
By :
SeedNew |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
บรรทัดแรกสุดครับ เป็นการ SELECT Database ครับ
|
 |
 |
 |
 |
Date :
2012-06-25 15:24:07 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|