sql-server-2005

query with join across multiple databases-syntax error

被刻印的时光 ゝ 提交于 2019-12-20 04:11:42
问题 I have 2 databases namely db1,db2. I need a query that fetch the data from these dbs(db1,db2) which have inturn 2 tables(concessions,invoicing) each. In db1.concessions => concession is primary key. db1.invoicing => [Concession Number] is primary key similarly in db2.concessions => concession is primary key. db2.invoicing => [Concession Number] is primary key In database1 db1.tbl1 => Concessions table has data concession TH-123 TH-456 FP-789 NZ-609 db1.tbl2 => invoicing table has data

SQL Server 2005 db_denydatawriter example query

十年热恋 提交于 2019-12-20 03:52:21
问题 I'm trying to add mydomain\myuser to the db_denydatawriter role but i can find a simple example of the query does anybody have a quick example? 回答1: 3 steps, in case you haven't set up login + user already CREATE LOGIN [mydomain\myuser] FROM WINDOWS; at the server level. MSDN CREATE USER [mydomain\myuser] FROM LOGIN [mydomain\myuser]; at the db level. MSDN Match user to role EXEC sp_addrolemember 'mydomain\myuser', 'db_denydatawriter' Edit: This only prevents INSERT, UPDATE and DELETE

Default table lock hint on SQL Server 2005/2008

谁说胖子不能爱 提交于 2019-12-20 03:28:31
问题 How do you look up a default global table locking hint? -- Questions Are there any DMV/DMF (Dynamic Management View/Function) that return such information? And also, is there a way to change the default lock hint? Currently I am adding nolock hint almost everywhere to prevent locks. I'd like to avoid doing so by changing the default lock hint to nolock so that existing stored procedures do not need to change. 回答1: I am not aware of any such global setting. IMHO even should that exist there

Summarise by week, even for empty rows

℡╲_俬逩灬. 提交于 2019-12-20 03:23:12
问题 I want to summarise the sum of sales.quantity by week, and to show the week number even if there are no sales. I have setup a weeks table with 1-54 in there to use an outer join to force all week numbers to come through, but it isn't working. It misses out weeks where there have been no sales. My query is: SELECT Weeks.WeekNum, SUM(sales.quantity) AS sales FROM Weeks LEFT OUTER JOIN sales ON Weeks.WeekNum = DATEPART(week, sales.transDate) WHERE (sales.transDate BETWEEN @fromDate AND @toDate)

Ledger Report Logic in Stored Procedure

☆樱花仙子☆ 提交于 2019-12-20 03:17:53
问题 i have a Stored Procedure called "Patient Ledger Report" where i need to show the day to day transaction details and balance amount of the patients.i was providing you one sampled data from below code how the data was inserting into my temporary table in my sp. create table #Patient_ledger (PATIENT_NAME varchar(250),PATIENT_NBR bigint,BILLNO varchar(250),BILLAMOUNT bigint, PAID_AMOUNT bigint) Insert into #Patient_ledger (Patient_name ,Patient_nbr ,billno ,billamount , paid_amount ) select

deleting records from multiple tables at a time with a single query in sqlserver2005

泪湿孤枕 提交于 2019-12-20 03:14:41
问题 I wanna delete records from child tables as well as parent table with in a single query. please find the query given below. here response header is the primary table and responseid is the primary key. DELETE FROM responseheader FROM responseheader INNER JOIN responsepromotion ON responseheader.responseid = responsepromotion.ResponseID INNER JOIN responseext ON responsepromotion.ResponseID=responseext.ResponseID WHERE responseheader.responseid In ('67D8B9E8-BAD2-42E6-BAEA-000025D56253') but

Using sp_executesql with params complains of the need to declare a variable

一世执手 提交于 2019-12-20 03:13:05
问题 I am attempting to make a stored procedure that uses sp_executesql. I have looked long and hard here, but I cannot see what I am doing incorrectly in my code. I'm new to stored procedures/sql server functions in general so I'm guessing I'm missing something simple. The stored procedure alter happens fine, but when I try run it I'm getting an error. The error says. Msg 1087, Level 15, State 2, Line 3 Must declare the table variable "@atableName" The procedure looks like this. set ANSI_NULLS ON

Finding a Primary Key Constraint on the fly in SQL Server 2005

时光怂恿深爱的人放手 提交于 2019-12-20 02:46:30
问题 I have the following SQL: ALTER TABLE dbo.PS_userVariables DROP CONSTRAINT PK_PS_userVariables; ALTER TABLE dbo.PS_userVariables ADD PRIMARY KEY (varnumber, subjectID, userID, datasetID, listid, userVarTitle); Since I have multiple environments, that PK_PS_userVariables constraint name is different on my different databases. How do I write a script that gets that name then adds it into my script? 回答1: While the typical best practice is to always explicitly name your constraints, you can get

Get the last word of a part of a varchar (LEFT/RIGHT)

白昼怎懂夜的黑 提交于 2019-12-20 02:38:05
问题 What is the correct way to get the last word of a part of a varchar? DECLARE @desc varchar(100) SET @desc='EXCHANGEUNIT P1i / SILVERBLACK/ CYRILLIC' SELECT RTRIM(LEFT(@desc, CHARINDEX('/', @desc) - 1)) This returns: EXCHANGEUNIT P1i I need to get only P1i with the query. Thanks in advance. 回答1: Use a combination of REVERSE, LEFT and CHARINDEX - like so: DECLARE @desc varchar(100) SET @desc='EXCHANGEUNIT P1i / SILVERBLACK/ CYRILLIC' SET @subdesc=RTRIM(LEFT(@desc, CHARINDEX('/', @desc) - 1))

SQL statement to select from 2 different tables, from two different databases (same server)

冷暖自知 提交于 2019-12-20 02:34:38
问题 How do I select from multiple tables in different databases on the same server? Also, Is there a way to have an identifying marker so I could see where the results came from? So for example: SELECT db1.table1.name, db2.table2.name, fromTbl FROM db1.table1, db2.table2 WHERE db1.table1.name LIKE '%j%' OR db2.table2.name LIKE '%j%' So in this case, I'm selecting the names from 2 different databases and tables. I'm doing a wildcard search on those names and the fromTbl would let me know where the