How to define mdf,ldf files default location path in SQL script - tsql

I want to give the script below to my client for installing a new database.
How can i rewrite this part of script to reflect their specific data and log file locations:
USE [master]
GO
/****** Object: Database [PhoneBook] Script Date: 2016/1/13 11:02:34 AM ******/
CREATE DATABASE [PhoneBook]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'PhoneBook', FILENAME = N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\PhoneBook.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'PhoneBook_log', FILENAME = N'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\PhoneBook_log.ldf' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
My client's windows drive is not c.
Can i change these paths to reflect their sql installation path?
How can i tell this script find the right location?

USE [master]
GO
DECLARE #mdfPath NVARCHAR(max), #ldfPath NVARCHAR(max) , #SQL NVARCHAR(MAX), #instName NVARCHAR(max) = 'PhoneBook'
SELECT #mdfPath = SUBSTRING(physical_name, 1,CHARINDEX(N'master.mdf',LOWER(physical_name)) - 1)+#instName+N'.mdf'
,#ldfPath = SUBSTRING(physical_name, 1,CHARINDEX(N'master.mdf',LOWER(physical_name)) - 1)+#instName+N'.ldf'
FROM master.sys.master_files
WHERE database_id = 1 AND FILE_ID = 1
SELECT #SQL =
'CREATE DATABASE [PhoneBook]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'''+#instName+''', FILENAME = N'''+#mdfPath+''' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'''+#instName+'_log'', FILENAME = N'''+#ldfPath+''' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)'
PRINT(#SQL)
EXECUTE(#SQL)
GO

For SQL Server 2012 and above, you can use these Server properties as found here as long as their default data and log file directories are set at the server level. You could do something like this using dynamic sql:
USE [master]
GO
DECLARE #defaultDataPath NVARCHAR(MAX) = CONVERT(VARCHAR(MAX), SERVERPROPERTY('InstanceDefaultDataPath'))
DECLARE #defaultLogPath NVARCHAR(MAX) = CONVERT(VARCHAR(MAX), SERVERPROPERTY('InstanceDefaultLogPath'))
DECLARE #sql NVARCHAR(MAX) =
'CREATE DATABASE [PhoneBook]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N''PhoneBook'', FILENAME = N''' + #defaultDataPath + 'PhoneBook.mdf'' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N''PhoneBook_log'', FILENAME = N''' + #defaultLogPath + 'PhoneBook_log.ldf'' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) '
PRINT #sql
EXEC (#sql)

Related

ROWTYPE in CURSOR postgresql

i want to know if it is possible to get "Rowtype" for a cursor in postgresql (pl/pgsql).
..
my cursor has a select from multiple table and i don't know how to get the rowtype.
c_Data CURSOR FOR
select
dst_description
, cnt_number
, prd_name
, grt_code
, res_currency
, res_date
, prm_installmentdate
from tbl_xxx, tbl_yyy, tbl_aaa
where cnt_id = res_xxx
and prd_id = cnt_yyy
and dst_id = prd_aaa
and grt_id = res_xxx
and prm_id = res_aaa;
l_Data c_Data%rowtype;
please help
More like this way round.
declare
l_data record;
begin
select
INTO l_data --<<
dst_description
, cnt_number
, prd_name
, grt_code
, res_currency
, res_date
, prm_installmentdate
from tbl_xxx, tbl_yyy, tbl_aaa
where cnt_id = res_xxx
and prd_id = cnt_yyy
and dst_id = prd_aaa
and grt_id = res_xxx
and prm_id = res_aaa;
l_data will then be of the rowtype you need.

sp_send_dbmail sends empty results

When using sp_send_dbmail like this :
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'MY_PROFILE'
,#recipients = 'MY_EMAIL_ADDRESS'
,#query = 'SELECT TOP 50 FIELD1, FIELD2, FIELD3 FROM TABLE1'
,#subject = 'MY_SUBJECT'
,#body = 'This is a test'
,#attach_query_result_as_file = 1
,#query_attachment_filename = 'file.csv'
,#query_result_separator = ' '
;
The attached files is always empty. I tried my query outside of sp_send_dbmail and it works perfectly. I also tried to replace my query by SELECT 1 and then my attached file has data.
What could cause my query to return no data?
I found how to solve it. I had to specify the database before the table name. Something more like :
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'MY_PROFILE'
,#recipients = 'MY_EMAIL_ADDRESS'
,#query = 'SELECT TOP 50 FIELD1, FIELD2, FIELD3 FROM DATABADE.dbo.TABLE1'
,#subject = 'MY_SUBJECT'
,#body = 'This is a test'
,#attach_query_result_as_file = 1
,#query_attachment_filename = 'file.csv'
,#query_result_separator = ' '
;

Not permitted when the subquery has expression more than 1 value! in SQL Server 2008

First of all sorry for my english,
I'm trying to insert a record into a table ... the record is the difference between the project table and temp table ...
The code I came into is
insert into pro_updatelog
select *, #user_n, GETDATE()
from pro
where cod = (select cod
from (SELECT * FROM pro
EXCEPT
SELECT * FROM temp
UNION ALL
SELECT * FROM temp
EXCEPT
SELECT * FROM pro) as T1);
then merge the temp table and original table ...
Not sure what should I do ...
All I want is that if there is any differences between the original table and temp table get recorded the original values into pro_updatelog table ... it would be best if only updated value will be recorded in table but it doesn't matter if a full row will be recorded but I want the original values before the update recorded!
Any ideas would be much appreciated!
thanks
I have succeeded that by using merge ... how ever it will take much longer to complete the stored procedure but it will do as I want ->
insert into pro_updatelog
select * , #user_n, GETDATE() from
(SELECT * FROM pro
EXCEPT
SELECT * FROM temp
UNION ALL
SELECT * FROM temp
EXCEPT
SELECT * FROM pro) as T1;
merge pro_updatelog as tar
using pro as sor
on (tar.cod = sor.cod)
when matched then
update set
tar.cod = sor.cod ,
tar.name_pr = sor.name_pr ,
tar.name_pe = sor.name_pe ,
tar.en = sor.en ,
tar.ending = sor.ending ,
tar.b = sor.b ,
tar.date_p = sor.date_p ,
tar.nek = sor.nek ,
tar.date_kh = sor.date_kh ,
tar.mp = sor.mp ,
tar.mt = sor.mt ,
tar.no_p = sor.no_p ,
tar.mas = sor.mas ,
tar.mablag = sor.mablag ,
tar.np = sor.np ,
tar.nf = sor.nf ,
tar.nn = sor.nn ,
tar.hpp1 = sor.hpp1 ,
tar.hpp2 = sor.hpp2 ,
tar.hpp3 = sor.hpp3 ,
tar.hpp4 = sor.hpp4 ,
tar.hpp5 = sor.hpp5 ,
tar.hpp6 = sor.hpp6 ,
tar.hpp7 = sor.hpp7 ,
tar.hpp8 = sor.hpp8 ,
tar.hpf1 = sor.hpf1 ,
tar.hpf2 = sor.hpf2 ,
tar.hpf3 = sor.hpf3 ,
tar.hpf4 = sor.hpf4 ,
tar.hpf5 = sor.hpf5 ,
tar.hpf6 = sor.hpf6 ,
tar.hpf7 = sor.hpf7 ,
tar.hpf8 = sor.hpf8 ,
tar.mola = sor.mola ,
tar.name1 = sor.name1 ,
tar.name2 = sor.name2 ,
tar.name3 = sor.name3 ,
tar.name4 = sor.name4 ,
tar.name5 = sor.name5 ,
tar.name6 = sor.name6 ,
tar.mab_t = sor.mab_t ,
tar.zarib1 = sor.zarib1 ,
tar.zarib2 = sor.zarib2 ,
tar.zarib3 = sor.zarib3 ,
tar.zarib4 = sor.zarib4 ,
tar.datet1 = sor.datet1 ,
tar.datet2 = sor.datet2 ,
tar.grup = sor.grup ,
tar.mablag1 = sor.mablag1 ,
tar.cod_g = sor.cod_g ,
tar.cod_m = sor.cod_m ,
tar.user_n = #user_n ,
tar.[date]=getdate();

Needing help to improve some TSQL "not exists" query performance

I'm having an performance issue running a query on a table containing 750 000 entries. It takes between 15 to 20 seconds to execute, blocking access to the database during that time and creating lots of error logs (and angry customers, of course).
Here is the query:
DECLARE #FROM_ID AS UNIQUEIDENTIFIER = 'XXX'
DECLARE #TO_ID AS UNIQUEIDENTIFIER = 'YYY'
update tbl_share
set user_id = #TO_ID
where user_id = #FROM_ID
and not exists (
select *
from tbl_share ts
where ts.file_id = file_id
and ts.user_id = #TO_ID
and ts.corr_id = corr_id
and ts.local_group_id = local_group_id
and ts.global_group_id = global_group_id
)
I'm kind of stuck right now since my TSQL knowledge is limited.
I'm wondering if:
I should create a temporary table
I should select something else than "*"
I haven't lot of opportunities to run the tests since it's a production database and there are permanently 10-20 customers connected on day time.
Thanks for your help!
How about restructuring your code logic?
DECLARE #FROM_ID AS UNIQUEIDENTIFIER = 'XXX'
DECLARE #TO_ID AS UNIQUEIDENTIFIER = 'YYY'
IF NOT EXISTS (select *
from tbl_share ts
where ts.user_id = #TO_ID)
BEGIN
update tbl_share
set user_id = #TO_ID
where user_id = #FROM_ID
END
So, you are doing your check beforehand and do only updating the database in the case it is needed.
HTH
Let's start with optimizing the select.
Check query plans.
If that is the PK then is it fragmented?
select *
from tbl_share
where user_id = #FROM_ID
and not exists (
select *
from tbl_share ts
where ts.file_id = file_id
and ts.user_id = #TO_ID
and ts.corr_id = corr_id
and ts.local_group_id = local_group_id
and ts.global_group_id = global_group_id
)
select tUpdate.*
from tbl_share as tUpdate
left outer join tbl_share as tExists
on tUpdate.user_id = #FROM_ID
and tExists.user_id = #TO_ID
and tExists.file_id = tUpdate.file_id
and tExists.corr_id = tUpdate.corr_id
and tExists.local_group_id = tUpdate.local_group_id
and tExists.global_group_id = tUpdate.global_group_id
where tExists.user_id is null

The multifier identifier could not be bound in update statement

I am trying to execute this query on sql server 2008r2-
SELECT #lUpd1 = 'UPDATE ts1cust.dbo.t_grgr_xwalk
SET xwalk.WEB = info.WEB,xwalk.AVIVIA = info.AVIVIA,xwalk.MSP = info.MSP,xwalk.QO = info.QO
FROM ts1cust.dbo.t_plan_mspqo_info info, ts1cust.dbo.t_grgr_xwalk xwalk
WHERE info.PLANID = xwalk.ID_471'
but getting below mentioned error-
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "xwalk.WEB" could not be bound.
You're not allowed to use multi-part identifiers for the assignment side of columns in the SET clause.
SELECT #lUpd1 = 'UPDATE xwalk
SET WEB = info.WEB, --<-- no xwalk. here
AVIVIA = info.AVIVIA,
MSP = info.MSP,
QO = info.QO
FROM ts1cust.dbo.t_plan_mspqo_info info
INNER JOIN
ts1cust.dbo.t_grgr_xwalk xwalk
ON info.PLANID = xwalk.ID_471'
I've also taken the liberty of replacing the table reference with the alias you're using at the top of the UPDATE, and switched to using ANSI JOIN rather than ,
You alias your table t_grgr_xwalk as xwalk, so for the first expression Update Table You should use xwalk too.
SELECT #lUpd1 = 'UPDATE xwalk
SET WEB = info.WEB,
AVIVIA = info.AVIVIA,
MSP = info.MSP,
QO = info.QO
FROM ts1cust.dbo.t_plan_mspqo_info info, ts1cust.dbo.t_grgr_xwalk xwalk
WHERE info.PLANID = xwalk.ID_471'
you can try below:
SELECT #lUpd1 = 'UPDATE ts1cust.dbo.t_grgr_xwalk xwalk
SET xwalk.WEB = info.WEB,xwalk.AVIVIA = info.AVIVIA,xwalk.MSP = info.MSP,xwalk.QO = info.QO
FROM ts1cust.dbo.t_plan_mspqo_info info
WHERE info.PLANID = xwalk.ID_471'