① sql語言中命令「SELECT」的用法!!!
select 列名稱 from 表名稱 where 條件,其中列名稱也可以是*,代表查處全部列。
比如:
select * from 圖書表 where 圖書名=『計算機導論』 表示從圖書表中查出圖書名=『計算機導論』的書的全部信息
如:select 圖書名,地址,電話 from 圖書表 where 圖書名=『計算機導論』 表示從圖書表中查出圖書名=『計算機導論』的書的某幾個信息,圖書名,地址,電話~~
希望對你有用~~
祝早日成功!
② 我想把一個查詢結果用SQL發出郵件這個過程怎麼做
--1.啟用DatabaseMail擴展存儲過程
sp_configure'showadvancedoptions',1
GO
RECONFIGURE
GO
sp_configure'DatabaseMailXPs',1
GO
RECONFIGURE
GO
sp_configure'showadvancedoptions',0
GO
RECONFIGURE
GO
--2.添加account
execmsdb..sysmail_add_account_sp
@account_name='zhanghao'--郵件帳戶名稱SQLServer使用
,@email_address='[email protected]'--發件人郵件地址
,@mailserver_name='smtp.126.com'--郵件伺服器地址
,@mailserver_type='SMTP'--郵件協議SQL2005隻支持SMTP
,@port=25--郵件伺服器埠
,@username='zhanghao'--用戶名
,@password='mima'--密碼
--3.添加profile
execmsdb..sysmail_add_profile_sp
@profile_name='dba_profile'--profile名稱
,@description='dbamailprofile'--profile描述
,@profile_id=null
--4.映射account和profile
execmsdb..sysmail_add_profileaccount_sp
@profile_name='dba_profile'--profile名稱
,@account_name='zhanghao'--account名稱
,@sequence_number=1--account在profile中順序
--5.1發送文本郵件
execmsdb..sp_send_dbmail
@profile_name='dba_profile'
,@recipients='[email protected]'
,@subject='SQLServer郵件測試'
,@body='內容啊'
,@body_format='TEXT'
--5.2發送附件
EXECsp_send_dbmail
@profile_name='dba_profile',
@recipients='[email protected]',
@subject='這是附件',
@file_attachments='G:亂七八糟sql.txt'
--5.3發送查詢結果
EXECsp_send_dbmail
@profile_name='dba_profile',
@recipients='[email protected]',
@subject='這是查詢',
@query='select*fromtest.dbo.apo_city'
--6.查看郵件發送情況
select*fromsysmail_allitems
select*fromsysmail_mailitems
select*fromsysmail_event_log
--7.刪除郵件配置
Execmsdb..sysmail_delete_profileaccount_sp
@profile_name='dba_profile',
@account_name='zhanghao'
Execmsdb..sysmail_delete_profile_sp
@profile_name='dba_profile'
Execmsdb..sysmail_delete_account_sp
@account_name='zhanghao'
③ sql server 2005 如何用存儲過程發送郵件
A.
使用簡單過程
以下存儲過程將從視圖中返回所有雇員(提供姓和名)、職務以及部門名稱。此存儲過程不使用任何參數。
復制
USE
AdventureWorks;
GO
IF
OBJECT_ID
(
'HumanResources.uspGetAllEmployees',
'P'
)
IS
NOT
NULL
DROP
PROCEDURE
HumanResources.uspGetAllEmployees;
GO
CREATE
PROCEDURE
HumanResources.uspGetAllEmployees
AS
SET
NOCOUNT
ON;
SELECT
LastName,
FirstName,
JobTitle,
Department
FROM
HumanResources.vEmployeeDepartment;
GO
uspGetEmployees
存儲過程可通過以下方式執行:
復制
EXECUTE
HumanResources.uspGetAllEmployees;
GO
--
Or
EXEC
HumanResources.uspGetAllEmployees;
GO
--
Or,
if
this
procere
is
the
first
statement
within
a
batch:
HumanResources.uspGetAllEmployees;
B.
使用帶有參數的簡單過程
下面的存儲過程只從視圖中返回指定的雇員(提供名和姓)及其職務和部門名稱。此存儲過程接受與傳遞的參數精確匹配的值。
復制
USE
AdventureWorks;
GO
IF
OBJECT_ID
(
'HumanResources.uspGetEmployees',
'P'
)
IS
NOT
NULL
DROP
PROCEDURE
HumanResources.uspGetEmployees;
GO
CREATE
PROCEDURE
HumanResources.uspGetEmployees
@LastName
nvarchar(50),
@FirstName
nvarchar(50)
AS
SET
NOCOUNT
ON;
SELECT
FirstName,
LastName,
JobTitle,
Department
FROM
HumanResources.vEmployeeDepartment
WHERE
FirstName
=
@FirstName
AND
LastName
=
@LastName;
GO
uspGetEmployees
存儲過程可通過以下方式執行:
復制
EXECUTE
HumanResources.uspGetEmployees
N'Ackerman',
N'Pilar';
--
Or
EXEC
HumanResources.uspGetEmployees
@LastName
=
N'Ackerman',
@FirstName
=
N'Pilar';
GO
--
Or
EXECUTE
HumanResources.uspGetEmployees
@FirstName
=
N'Pilar',
@LastName
=
N'Ackerman';
GO
--
Or,
if
this
procere
is
the
first
statement
within
a
batch:
HumanResources.uspGetEmployees
N'Ackerman',
N'Pilar';
C.
使用帶有通配符參數的簡單過程
以下存儲過程只從視圖中返回指定的一些雇員(提供名和姓)及其職務和部門名稱。此存儲過程模式與所傳遞的參數相匹配;或者,如果未提供參數,則使用預設的默認值(以字母
D
打頭的姓)。
復制
USE
AdventureWorks;
GO
IF
OBJECT_ID
(
'HumanResources.uspGetEmployees2',
'P'
)
IS
NOT
NULL
DROP
PROCEDURE
HumanResources.uspGetEmployees2;
GO
CREATE
PROCEDURE
HumanResources.uspGetEmployees2
@LastName
nvarchar(50)
=
N'D%',
@FirstName
nvarchar(50)
=
N'%'
AS
SET
NOCOUNT
ON;
SELECT
FirstName,
LastName,
JobTitle,
Department
FROM
HumanResources.vEmployeeDepartment
WHERE
FirstName
LIKE
@FirstName
AND
LastName
LIKE
@LastName;
GO
uspGetEmployees2
存儲過程可使用多種組合執行。下面只顯示了幾個組合:
復制
EXECUTE
HumanResources.uspGetEmployees2;
--
Or
EXECUTE
HumanResources.uspGetEmployees2
N'Wi%';
--
Or
EXECUTE
HumanResources.uspGetEmployees2
@FirstName
=
N'%';
--
Or
EXECUTE
HumanResources.uspGetEmployees2
N'[CK]ars[OE]n';
--
Or
EXECUTE
HumanResources.uspGetEmployees2
N'Hesse',
N'Stefen';
--
Or
EXECUTE
HumanResources.uspGetEmployees2
N'H%',
N'S%';
D.
使用
OUTPUT
參數
以下示例將創建
uspGetList
存儲過程。此過程將返回價格不超過指定數值的產品的列表。此示例顯示如何使用多個
SELECT
語句和多個
OUTPUT
參數。OUTPUT
參數允許外部過程、批處理或多條
Transact-SQL
語句在過程執行期間訪問設置的某個值。
復制
USE
AdventureWorks;
GO
IF
OBJECT_ID
(
'Proction.uspGetList',
'P'
)
IS
NOT
NULL
DROP
PROCEDURE
Proction.uspGetList;
GO
CREATE
PROCEDURE
Proction.uspGetList
@Proct
varchar(40)
,
@MaxPrice
money
,
@ComparePrice
money
OUTPUT
,
@ListPrice
money
OUT
AS
SET
NOCOUNT
ON;
SELECT
p.[Name]
AS
Proct,
p.ListPrice
AS
'List
Price'
FROM
Proction.Proct
AS
p
JOIN
Proction.ProctSubcategory
AS
s
ON
p.ProctSubcategoryID
=
s.ProctSubcategoryID
WHERE
s.[Name]
LIKE
@Proct
AND
p.ListPrice
④ SQL問題,我在資料庫中用sp_send_dbmail發送了500多封郵件給自己,但是只收到100多封,請問怎麼檢查呢~
打開企業管理器,在對象資源管理器--管理--資料庫郵件,右鍵,「查看資料庫郵件日誌」
或者在sql查詢分析器運行
use msdb
go
select * from sysmail_allitems
select * from sysmail_mailitems
select * from sysmail_event_log
⑤ SQL之SELECT語句
檢索數據的SQL語句,就是SELECT語句。
使用SELECT語句可以從表中檢索出一個或多個數據列。
如何使用SELECT語句,我們應該告訴SELECT語句從什麼地方選擇什麼。
這是一個簡單的SELECT語句,表示從procts表內檢索一個名為prod_name的列。
所需要的列名在SELECT關鍵字後給出,FROM關鍵字指出檢索數據的表名。
要想從一個表中檢索多個列,使用相同的SELECT語句。唯一的不同是必須在SELECT關鍵字後給出多個列名,列名之間必須以逗號分隔。
SELECT語句通過在列名的位置使用星號(*)通配符,可以檢索所有的列而不必逐個列出它們。
SELECT語句返回所有匹配的行,它們可能是指定表中的每個行。為了返回第一行或前幾行,可使用LIMIT子句。
⑥ SQL郵件發送
你的郵件伺服器上可能不支持ssl加密但是你的賬戶配的時候選擇了SSL加密 否則反之 你去你的賬戶中查看你的SSL加密即可
⑦ SQL 發送郵件可以做作業嗎
業務場景:
業務資料庫數據達到一定數量級後,進行郵件發送提醒。使用SQL Server Management Studio連接到資料庫伺服器後,可按下面步驟創建一個作業,通過作業執行特定語句,進行郵件發送。
(1)、在SQL Server Management Studio的資料庫實例的管理菜單下,選中SQL Server代理→作業菜單,右擊作業菜單,然後點擊「新建作業」。
(2)、在作業屬性/常規屬性頁,維護好作業的名稱等信息。
(3)、在作業屬性/步驟屬性頁,維護作業步驟及作業命令。
本文示例SQL:
USE msdb
GO
DECLARE @cnt INT;
DECLARE @emailBody VARCHAR(4000);
SELECT @cnt=COUNT(*) FROM ng0002.dbo.secuser;
IF @cnt>100
BEGIN
SELECT @emailBody='資料庫用戶表記錄數已達:
'+convert(varchar,@cnt) ;
Exec dbo.sp_send_dbmail
@profile_name='SQLMailConfig',
@recipients='[email protected]',
@subject='用戶數預警',
@body=@emailBody
END
(4)、在作業屬性/計劃屬性頁,維護作業的執行計劃。
(5)、待資料庫作業成功執行後,將會收到一份類似下圖的郵件:
⑧ sql發送郵件問題郵件怎麼判斷他是否發送成功
查看所有發給danw但是未成功的記錄USE msdb ;
GO
SELECT items.subject,
items.last_mod_date
,l.description FROM dbo.sysmail_faileditems as items
INNER JOIN dbo.sysmail_event_log AS l
ON items.mailitem_id = l.mailitem_id
WHERE items.recipients LIKE '%danw%'
OR items._recipients LIKE '%danw%'
OR items.blind__recipients LIKE '%danw%'
GO
⑨ sql 中怎麼把數據弄成報表進行郵件發送
SQL 本身就可以發送郵件
https://msdn.microsoft.com/zh-cn/library/ms190307(v=sql.90).aspx
將你的數據變成附件類型的報表,再自動發送
⑩ sql發郵件@query字元串拼接
set @query =' select * from b where [數據]> (select [數據] from a where convert(varchar(10),orderTime,120)=convert(varchar(7),orderTime,120)+'+char(ascII('+'))+char(39)+01+char(39)+')'