Thank this author by sharing:
By Sushila Iyer, 2005/09/26
Let's learn how to create tables, views and stored procedures using both EM and QA to our advantage:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EmpDetails]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[EmpDetails] GO CREATE TABLE [dbo].[EmpDetails] ( [empPK] [int] IDENTITY (1, 1) NOT NULL , [empFirstName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [empLastName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[EmpDetails] WITH NOCHECK ADD CONSTRAINT [PK_EmpDetails] PRIMARY KEY CLUSTERED ( [empPK] ) ON [PRIMARY] GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EmpAccounts]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[EmpAccounts] GO CREATE TABLE [dbo].[EmpAccounts] ( [empFK] [int] NOT NULL , [empAccountNumber] [int] NULL , [empBankName] [varchar] (50) NULL ) ON [PRIMARY] GO
Now for views:
SELECT FROM dbo.EmpDetails CROSS JOIN dbo.EmpAccounts
SELECT FROM dbo.EmpDetails INNER JOIN dbo.EmpAccounts ON dbo.EmpDetails.empPK = dbo.EmpAccounts.empFK
SELECT dbo.EmpDetails.empPK, dbo.EmpDetails.empFirstName, dbo.EmpDetails.empLastName, dbo.EmpAccounts.empFK, dbo.EmpAccounts.empAccountNumber, dbo.EmpAccounts.empBankName FROM dbo.EmpDetails INNER JOIN dbo.EmpAccounts ON dbo.EmpDetails.empPK = dbo.EmpAccounts.empFK
SELECT dbo.EmpDetails.empPK, dbo.EmpDetails.empFirstName + ' ' + dbo.EmpDetails.empLastName AS empName, dbo.EmpAccounts.empFK, dbo.EmpAccounts.empAccountNumber, dbo.EmpAccounts.empBankName FROM dbo.EmpDetails INNER JOIN dbo.EmpAccounts ON dbo.EmpDetails.empPK = dbo.EmpAccounts.empFK
SELECT dbo.EmpDetails.empPK, dbo.EmpDetails.empFirstName + ' ' + dbo.EmpDetails.empLastName AS empName, dbo.EmpAccounts.empFK, dbo.EmpAccounts.empAccountNumber, dbo.EmpAccounts.empBankName FROM dbo.EmpDetails LEFT OUTER JOIN dbo.EmpAccounts ON dbo.EmpDetails.empPK = dbo.EmpAccounts.empFK
INSERT INTO EmpAccounts (empFK, empAccountNumber, empBankName) VALUES(1, 1000, 'Wachovia') INSERT INTO EmpAccounts (empFK, empAccountNumber, empBankName) VALUES(2, 1001, 'Sun Trust') INSERT INTO EmpAccounts (empFK, empAccountNumber, empBankName) VALUES(3, 1002, 'Amro Bank') INSERT INTO EmpAccounts (empFK, empAccountNumber, empBankName) VALUES(4, 1003, 'Credite Suisse') INSERT INTO EmpAccounts (empFK, empAccountNumber, empBankName) VALUES(5, 1004, 'Central Fidelity') INSERT INTO EmpAccounts (empFK, empAccountNumber, empBankName) VALUES(6, 1005, 'Wachovia')
Now for the very last object - stored procedure:
/************************************************************************************ Name: procEmpInfo Details: View employee name, account number and bank info from EmpDetails & EmpAccounts tables. Date Who Comments History: 08/10/2005 Sushila Created initial. ************************************************************************************/ CREATE PROCEDURE procEmpInfo AS SELECT * FROM vwEmpInfo GO
I need to create a multi tag XML from select
how to create a SP with select inside
Export option (right click in the database - tasks - export data...)
Select second highest wage
Creating PK
As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.
Join us!
Steve Jones Editor, SQLServerCentral.com