Quantcast
Channel: Transact-SQL forum
Viewing all 12890 articles
Browse latest View live

DDL Logging in each db of a server

$
0
0

Hi all,

We are dealing with sql server 2008 and 2012 servers. We already have a method to log ddl events. We are using DDL triggers to log the event informations to "DDLLog" table. We have created DDL trigger in each db and logging information. This has been applied long back it seems. So, TL asked me to find a better way to log DDL info.

I have searched about this and I could see that there are 2 other ways we can do this. Extended Eventsand Service Broker.  I'm new to this and I have no enough idea about triggers also.

Please help me to find how can I apply this in each db of a server ?


UNION ALL HELPS

$
0
0

I would like to create a header or dummy record by using SELECT A, B, C, etc and then I use UNION ALL to select my detail record.

If I don't UNION ALL to my header record, my detail record works the way I wanted but when I used UNION ALL with my header record, SQL is complaining about my ORDER BY in my detail record selection.

How can I get around this error?  Is there another way to create a header/dummy record without using UNION ALL?

Thank you very much for all your helps.

Msg 104, Level 16, State 1, Line 183

ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.

My code is as follow:

DECLARE  @EventIDs INT
SET @EventIDs = 24065

DECLARE @ReportSortOrder VARCHAR(50)
SET @ReportSortOrder='Defendant Name'--'DEFENDANT NAME'
SET @ReportSortOrder='Violation Date'--'BY VIOLATION DATE'    
SET @ReportSortOrder='Officer'--'BY OFFICER'  
SET @ReportSortOrder='Case Number' --BY CASE NUMBER

    
DECLARE @SealPin INT
SET @SealPin = (SELECT TOP 1 ls.LookupKey FROM tblLookup ls WITH(NOLOCK) 
                        WHERE ls.LookupGroup='ReportVariables'
                        AND ls.Code='_ClerkSealPin')

DECLARE @CourtAddressPIN INT
SET @CourtAddressPIN = CONVERT(INT, (SELECT TOP 1 LookupKey FROM tblLookup WITH(NOLOCK) WHERE LookupGroup = 'reportvariables' AND Code = '_courtpin'))


SELECT
  
  RecordType = 'Header'

  ,CourtPINProperName = dbo.fnGetProperNameByPartyID(@CourtAddressPIN)
  ,FormCaseNumber = ''
  ,CaseID ='',
  CaseNumber = '',
  CourtResult = '',
  CaseStatus = '',
  CaseStatusDescription = '',
  
  JudgeID = '',
  Disposition = '',
  DispositionCode = '',
  DispositionDate = '',
  FirstDefendantID = '',
  
  AmountPaid = 0,
					
  TotalChargesPerCharge = 0,

  ReceiptNumber = 0
  
  
UNION ALL


SELECT

  RecordType = 'Detail'
  ,CourtPINProperName = dbo.fnGetProperNameByPartyID(@CourtAddressPIN)
  ,FormCaseNumber = c.CaseNumber
  ,c.CaseID,
  c.CaseNumber,
  ce.CourtResult,
  c.CaseStatus,
  casestatus.Description CaseStatusDescription,
  
  c.JudgeID,
  c.Disposition,
  c.DispositionCode,
  c.DispositionDate,
  c.FirstDefendantID,
  
  AmountPaid = (SELECT SUM(cf1.AmountPaid) FROM tblCase c1 with(nolock)
					LEFT OUTER JOIN tblCaseFee cf1 
					ON c1.CaseID = cf1.CaseID WHERE c1.CaseID = c.CaseID),
					
  ISNULL((SELECT ISNULL(SUM(tcpccf.TotalFee),0) FROM tblCase tcpcc WITH(NOLOCK)
		INNER JOIN tblCaseCharge tcpccc WITH(NOLOCK)
			ON tcpcc.CaseID = tcpccc.CaseID
		LEFT OUTER JOIN tblCaseFee tcpccf WITH(NOLOCK)
			ON tcpccf.CaseChargeID = tcpccc.CaseChargeID
			
				WHERE tcpcc.CaseID = c.CaseID AND tcpccc.CaseChargeID = cc.CaseChargeID

			GROUP BY tcpccc.CaseChargeID),0) TotalChargesPerCharge,

  ISNULL((SELECT TOP 1 t.ReceiptNumber from tblTransactionDetail td WITH (NOLOCK)
				INNER JOIN tblTransaction t WITH (NOLOCK)
					ON td.TransactionID = t.TransactionID
			WHERE td.CaseID = c.CaseID ORDER BY t.CashierDate DESC),0) ReceiptNumber		

  
FROM
  tblCase c WITH(NOLOCK) 
  INNER JOIN tblCourtType crt WITH(NOLOCK)
    ON c.CourtTypeID = crt.CourtTypeID  
  INNER JOIN tblCaseCharge cc WITH(NOLOCK)
    ON c.CaseID = cc.CaseID
  
  INNER JOIN tblCaseEvent ce WITH(NOLOCK)
    ON c.CaseID = ce.CaseID
  
  INNER JOIN tblEvent ev WITH(NOLOCK)
    ON ce.EventID = ev.EventID
        
  LEFT OUTER JOIN tblCitation cit WITH(NOLOCK)
       ON cc.CitationID = cit.CitationID      
  LEFT OUTER JOIN tblLookup cfp WITH(NOLOCK)
    ON cc.CourtFinalPlea = cfp.Code AND cfp.LookupGroup = 'CourtFinalPlea'
  
  LEFT OUTER JOIN tblLookup casestatus WITH(NOLOCK)
    ON c.CaseStatus = casestatus.Code AND casestatus.LookupGroup = 'CaseStatus'
        
  LEFT OUTER JOIN tblLookup cat WITH(NOLOCK)
    ON cc.CourtActionTaken = cat.Code AND cat.LookupGroup = 'CourtActionTaken'    
  LEFT OUTER JOIN tblCaseBond cb WITH(NOLOCK)
    ON cc.CaseID = cb.CaseID AND cc.CaseChargeID = cb.CaseChargeID AND cb.BondStatus LIKE 'POSTED'
    
  LEFT OUTER JOIN tblCourtTypeCourtResult ctcrn WITH(NOLOCK)
	ON c.CourtTypeID = ctcrn.CourtTypeID AND ce.CourtResult = ctcrn.CourtResult
						      
  LEFT OUTER JOIN tblLookup l WITH(NOLOCK) 
    ON l.Code = ev.CourtRoomCode AND l.lookupgroup = 'courtroom'   
  
  LEFT OUTER JOIN tblEventType et WITH(NOLOCK)
    ON ce.CaseEventTypeID = et.EventTypeID
  LEFT OUTER JOIN tblStatute st  WITH (NOLOCK)
    ON 
      (
      SELECT 
        CASE cc.ChargePhase
          WHEN 'I' THEN cc.InitialStatuteID
          WHEN 'P' THEN cc.ProsecutorStatuteID
          WHEN 'C' THEN cc.CourtStatuteID
          ELSE cc.StatuteID
        END
      )  = st.StatuteID
  INNER JOIN tblPartyName pn WITH(NOLOCK)
    ON c.FirstDefendantID = pn.PartyID
  LEFT OUTER JOIN tblLookup lc WITH(NOLOCK)
    ON c.CustodyLocationCode = lc.Code AND lc.LookupGroup='CustodyLocation'
  --  Court Results for Selected CaseEvent
  LEFT OUTER JOIN tblCourtTypeCourtResult ctcr WITH(NOLOCK)
    ON c.CourtTypeID = ctcr.CourtTypeID AND
       ce.CourtResult = ctcr.CourtResult
    
  LEFT OUTER JOIN tblCaseParty cp WITH(NOLOCK)
       ON c.CaseID = cp.CaseID
  LEFT OUTER JOIN tblCaseParty pcp WITH(NOLOCK)
       ON cp.ParentCasePartyID = pcp.CasePartyID       
  LEFT OUTER JOIN tblParty p WITH(NOLOCK)
       ON c.FirstDefendantID = p.PartyID
  --LEFT OUTER JOIN tblParty p WITH(NOLOCK)
  --     ON cp.PartyID = p.PartyID       
  LEFT OUTER JOIN tblCasePartyType cpt WITH (NOLOCK)
      ON c.CourtTypeID = cpt.CourtTypeID AND cp.CasePartyType = cpt.CasePartyType         
       
      
  OUTER APPLY dbo.fnGetCasePartyAddressByCasePartyIDPartyID(cp.CasePartyID,cp.PartyID) cpacp
  OUTER APPLY dbo.fnGetCaseEventCourtRoomAddress(ev.CourtRoomCode) cecra
  
  OUTER APPLY dbo.fnGetNumberOfCasePartyTypesByCaseParty(c.CaseID, cp.CasePartyType) gncpp
  OUTER APPLY dbo.fnGetDepartmentPartyInfoByCourtType(crt.CourtType) dpi 
  OUTER Apply dbo.fnGetOrdinalDateSuffix(GetDate()) gods
  
          
WHERE


  (ce.EventID IN(@EventIDs)) AND
  (ce.CourtResult NOT IN ('CONTINUED','COMPLETED')) AND
  (CaseStatus.Description NOT IN ('CLOSED'))
  --ISNULL(ctcr.Disposed, 0) = 0  --AND  -- Exclude Disposed Case Events for Selected Event


ORDER BY
  CASE @ReportSortOrder
    WHEN 'Case Number'  THEN c.CaseNumber + ' -- ' + dbo.fnGetDisplayNameByPartyID(c.FirstPlaintiffID) + ' -- ' + (CONVERT (VARCHAR(MAX), c.FirstPlaintiffID))
    WHEN 'Defendant Name'  THEN dbo.fnGetDisplayNameByPartyID(c.FirstDefendantID) + ' -- ' + (CONVERT (VARCHAR(MAX), c.FirstDefendantID))
    WHEN 'Violation Date'  THEN CONVERT(VARCHAR,cc.OffenseDate,23) + ' -- ' + (CONVERT (VARCHAR(MAX), c.FirstDefendantID))
    WHEN 'Officer'  THEN dbo.fnGetDisplayNameByPartyID(
												(SELECT TOP 1 cpd.PartyID 
														FROM 
															tblCaseParty cpd WITH(NOLOCK)
																WHERE cpd.CaseID = c.CaseID AND 
																	cpd.CasePartyType = 'OFF')) + ' -- ' + (CONVERT (VARCHAR(MAX), c.FirstDefendantID))
    ELSE ''
   END ASC

Database stuck in "In Recovery" mode

$
0
0

Hello. I was doing a query on a database to clear the tables in it. A query that normally takes 2 minutes took the better part of 3 hours...and it wasnt even close to being complete. Therefore, i killed SQL in task manager cause i felt like i had no other choice.

Upon logging back in, the database has been (in Recovery). Accoring to various post i read online, simply detaching it and reattaching it should work fine however, when i try to run queries to detach it i get the following error.

Alter database failed because a lock couldnt be obtained for database <databasename>

I also read that this was due to a connection not being terminated but....everything i tried gives that same lock error. I made sure i was in master, i reset the pc, un-plug network table, tried the commands in the GUI. Nothing's letting me detach said database or remove it from In recovery.

Any help would be appreciated..thank you

Restrict insert of one user's event information to DDLLog table

$
0
0

Hi All,

I have created a DDL trigger to log the DDL event informations to DDLLog table.I need to rerstrict a user log information from DDL log table..how do I exclude that data while inserting?

For ex:

Windows user  called "xyz\julie" will make lot of changes in database. But i don't want to log that information in DDL log table. But I need all other user's event information to DDLog table.  How do I restrict this action?

If I deny this user for inserting data to DDLLog table will produce error while making any ddl script.

Here is my trigger

USE [master]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


Alter TRIGGER [DDL_DB_Events]
ON ALL SERVER
--with execute as 'jsam'
FOR DDL_TABLE_EVENTS, DDL_VIEW_EVENTS, DDL_FUNCTION_EVENTS, DDL_PROCEDURE_EVENTS, DDL_TRIGGER_EVENTS, DDL_USER_EVENTS, DDL_ROLE_EVENTS,-- DDL_DATABASE_EVENTS,
DDL_SERVER_LEVEL_EVENTS
AS

SET NOCOUNT ON

DECLARE @EventInfo xml

SET @EventInfo = EVENTDATA()

INSERT master.dbo.DDLLog
(
EventDataXML
,[DatabaseName]
)
values

@EventInfo
,@EventInfo.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)')
)


GO

grant insert on dbo.DDLLog to public

Help in Joining query

$
0
0

Hi, How I can get desired result. I am joining two tables but if date timeline does't exist in #two table then should show hyphen (-). Please help on this query. Thanks.

create table #one (code_p char(4), code_h char(2), code_date datetime)

insert into #one values ('DEHG','2','2010-01-01')

insert into #one values ('DEHG','2','2011-01-01')

insert into #one values ('DEHC','2','2009-01-01')

insert into #one values ('DEHG','2','2012-01-01')

create table #two (code_p char(4), code_h char(2), code_date datetime)

insert into #two values ('DEHG','2','2010-01-01')

insert into #two values ('DEHC','2','2009-01-01')

select p.code_p code_p_one, p.code_h code_h_one, p.code_date code_date_one,

p.code_p code_p_two, p.code_h code_h_two, p.code_date code_date_two from #one p join #two a on p.code_p = a.code_p

--Result from the above query

code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two

DEHG     2    2010-01-01     DEHG     2     2010-01-01

DEHG     2    2011-01-01     DEHG     2     2010-01-01

DEHC     2    2009-01-01     DEHG     2     2009-01-01

DEHC     2    2012-01-01     DEHG     2     2009-01-01

--Desired result

code_p_one code_h_one code_date_one code_p_two code_h_two code_date_two

DEHG     2    2010-01-01     DEHG     2     2010-01-01

DEHG     2    2011-01-01     DEHG     2     -

DEHC     2    2009-01-01     DEHG     2     2009-01-01

DEHC     2    2012-01-01     DEHG     2     -

How do we format a date as "dd/mm/yy"

$
0
0

Hi everybody.

I wish to format a date as "dd/mm/yy"

For example, today should be 12/08/14  (2 digits for the year)

I got this formula from the forum:

DECLARE @today varchar(10)
SET @today = CONVERT(VARCHAR(10), GETDATE(), 103)
SELECT @today

It returns  12/08/2014.


Can we modify the formula to make it return 12/08/14 instead?

Note: I use SQL Server 2008. It seems FORMAT() does not work with this version.

Thanks

Leon Lai


UDF does not return value even though its underlying query does.

$
0
0

I have a User Defined Function which is supposed to return a scalar varchar(50) value.  This is what I did:

ALTER FUNCTION [dbo].[zt_prc_fnNSNsFmNIIN_Get]  
(
	@NIIN varchar(5),
	@PN varchar(20)
)
RETURNS Varchar(50)
AS
BEGIN
	-- Declare the return variable here
	Declare @NSN varchar(50)
	Set @NSN = (SELECT Convert(varchar(20), RTRIM(NSN.FSC)) + Convert(varchar(20), RTrim(NSN.NIIN)) NSN
				FROM TechManual.dbo.ANSNS NSN 
					JOIN TechManual.dbo.AITEMTABLE AIT ON NSN.PRIMARYPARTNUMBER = AIT.PRIMARYPARTNUMBER and NSN.DODIC = AIT.DODIC
					JOIN TechManual.dbo.PARTNUMBERS PN ON AIT.PRIMARYPARTNUMBER = PN.PRIMARYPARTNUMBER
				WHERE NSN.NIIN = @NIIN AND PN.PARTNUMBER = @PN)
	RETURN @NSN
END

However I keep getting a Null as the return.  So I ran a test to call the function and run the the underlying query like this:

Select dbo.zt_prc_fnNSNsFmNIIN_Get('015132378', '850AS911')
Declare @NSN varchar(50)
	Set @NSN = (SELECT Convert(varchar(20), RTRIM(NSN.FSC)) + Convert(varchar(20), RTrim(NSN.NIIN)) NSN
				FROM TechManual.dbo.ANSNS NSN 
					JOIN TechManual.dbo.AITEMTABLE AIT ON NSN.PRIMARYPARTNUMBER = AIT.PRIMARYPARTNUMBER and NSN.DODIC = AIT.DODIC
					JOIN TechManual.dbo.PARTNUMBERS PN ON AIT.PRIMARYPARTNUMBER = PN.PRIMARYPARTNUMBER
				WHERE NSN.NIIN = '015132378' AND PN.PARTNUMBER = '850AS911')
	Select @NSN

As you can see the Select statement is exactly what is entered in the UDF.  However the call to the UDF yields

null

and the call to select the variable populated by the query yields "1377015132378".

Everyone here has taken a look at the UDF and are unable to see anything which might be an issue.


Edward R. Joell MCSD MCDBA

Concatenation not working in T-SQL

$
0
0

I have a Stored Procedure in that stored procedure Changiing the bvalue of a variable like so:

Set @NewLine = @NewLine + 'AC' Select @NewLine

@NewLine is equal to '0001' Originally I tried to do this by

Set @NewLine += 'AC'

but I kept getting a result of '0001' So I changed it to the older form, 

Set @NewLine = @NewLine + 'AC'

But it still keeps returning '0001'


Edward R. Joell MCSD MCDBA


Remove text inside brackets

$
0
0

I have SQL Server 2008R2 with a SELECT query that removes brackets [ and ] and text in between using the TSQL below.  However, we now sometimes have 2 sets of brackets that we need to remove.  For example if L.DamageDescription contains "This is regular text [remove this] [also remove this]" I want it to return just "This is regular text".  How do I add to my code to do this? Also, I think I will end up with a UDF to get this for other queries.  Thanks.

SELECT 
CASE WHEN CHARINDEX('[', L.DamageDescription) > 0 THEN
		STUFF(L.DamageDescription,
			  CHARINDEX('[', L.DamageDescription),
			  CHARINDEX(']', L.DamageDescription) -
			  CHARINDEX('[', L.DamageDescription) + 1, '')
	 ELSE L.DamageDescription 
	 END AS NewDescription 
FROM dbo.RepairOrderLines

Join between three tables where data is in Comma values row ?

$
0
0

Category table 

CatID

Category

1

Wedding

2

Babies/Kids

3

Special Occasion

4

Corporate

5

Commercial

6

Fashion

7

Travel

Subactegory Table

SubCatID

CategoryID

SubCategory

1

1

Candid Wedding 

2

2

Baby First Birthday  

3

3

Birthday 

4

4

Conference 

5

5

Advertisement 

6

6

Model Portfolio 

7

1

Parsi Wedding 

8

1

Buddhist Wedding 

9

1

North Indian Wedding 

10

1

South Indian Wedding 

Main Table 

ScreenName

SubCategoryID

Forever22

2,5,10,12,13,25

war

4

Rinku

8,5

abcdefg

8,5

i want  data category wise  from the Main Table 

like form category table 5 is Commercial.

then the output should be 

ScreenName

Category

Forever22

2,5,10,12,13,25

Rinku

8,5

abcdefg

8,5

please help 

How to separate a column with a hyphen value into two parts? Please help!

$
0
0

Hi,
  I have a Employee table and there is a column called EmpID which has the values like 100AS-12U, 189722,123-yure12, qswer-23 . that means, some of the values have two parts with a - in between. The place of the - may vary from value to value. My requirement is, I have to create two computed columns from this. First column is before the - and the second column is after the -. if it does not have any - in the value (Eg:189722), it should go to the first column. Here are the DDL, with values and also the output required. Could you please help me to get the requirement done? thanks in advance!

---Drop table #Temp_Emp
---truncate table #Temp_Emp

Create table #Temp_Emp (EmpID varchar(30))
go

insert into #Temp_Emp  values ('100AS-12U')
insert into #Temp_Emp  values ('189722')
insert into #Temp_Emp  values ('123-yure12')
insert into #Temp_Emp  values ('qswer-23')

--select * from #Temp_Emp 

---Requirement

Select '100AS' as COl1,'12U' as COl2 union
Select '189722' as COl1,'' as COl2 union
Select '123' as COl1,'yure12' as COl2 union
Select 'qswer' as COl1,'23' as COl2

Need Help with CTE for Compound Interest Calculation

$
0
0

Hi

I need some help with a simple CTE query to calculate the Compound Interest +Principle 

Calculated Value=Compound Interest +Principle 

Row 2= 1000*0.05+1000=1050

Row 3=1050*0.05+1050=1102.50

valuerow_numrateCalculated Amount(Compound Interest +Principle )
1000.0010.051000.0000
1000.0020.051050.0000
1000.0030.051102.5000
1000.0040.051157.6250
1000.0050.071238.6588
1000.0060.071325.3649
1000.0070.081431.3941
1000.0080.081545.9056

--Dummy Data

CREATE TABLE #test (
[value] [decimal](18, 4) ,
[num_count] [int] ,
[rate] [decimal](18, 4)


insert into #test values (1000,1,0.05)
insert into #test values (1000,2,0.05)
insert into #test values (1000,3,0.05)
insert into #test values (1000,4,0.05)
insert into #test values (1000,5,0.07)
insert into #test values (1000,6,0.07)
insert into #test values (1000,7,0.08)
insert into #test values (1000,8,0.08)

Global temp table

$
0
0

Hi, I have created a global temp table ##tbl_ProductTypes and want to run in another databse but not able to run another database and getting the error 'Invalid object name '##tbl_ProductTypes. How I can run this. Please help. Thanks.

select * into ##tbl_ProductTypes from dbo.tbl_ProductTypes

SQL Filtering Query

$
0
0

Dear Experts,

I have a table in my SQL Database 2008, data shows like below

RowNumber    CCode      CDesc      Date

     1                 A01            A        2014-06-01
     2                 A01            A        2014-05-01
     1                 B01            B        2014-07-01
     2                 B01            B        2014-06-01
     1                 C01            C        2014-05-01
     2                 C01            C        2014-06-01
     3                 C01            C        2014-07-01
     1                 D01            D        2014-05-01
     2                 D01            D        2014-06-01
     3                 D01            D        2014-07-01
     4                 D01            D        2014-04-01
     1                 E01             E        2014-07-01
     2                 E01             E        2014-06-01

I have a SSRS 2008 report and I will be passing Row Number a parameter like (1,2,3,4) as a single value parameter.

If I pass 4 as parameter then that should pic list of all clients which has a row number of 4 and it's record.

for example from the above data if I pass the row number as 4 the I should see below output

 RowNumber    CCode      CDesc      Date

     1                 D01            D        2014-05-01
     2                 D01            D        2014-06-01
     3                 D01            D        2014-07-01
     4                 D01            D        2014-04-01

for example from the above data if I pass the row number as 3 the I should see below output

 RowNumber    CCode      CDesc      Date

     1                 C01            C        2014-05-01
     2                 C01            C        2014-06-01
     3                 C01            C        2014-07-01

for example from the above data if I pass the row number as 2 the I should see below output

 RowNumber    CCode      CDesc      Date
     1                 A01            A        2014-06-01
     2                 A01            A        2014-05-01
     1                 B01            B        2014-07-01
     2                 B01            B        2014-06-01
     1                 E01            E        2014-07-01
     2                 E01            E        2014-06-01

Any help will be much appreciated.

Thankyou.

Regards,

Jeevan.


Dasari


Dasari

Evaluate Multiple Columns in a Table

$
0
0

Hi,

I have a table like below

A B C D
X   
X X  
X X X X

My requriement is , i want to take the column name , where ever X is present for each row.If there are multiple columns with X, then they must be seperated by ;

Out put would be in 1 column only

For example the out put for the above table should be

Output

A

A;B

A;B;C;D

DDL

Declare @Table table
(A varchar(1) , B varchar(1),C varchar(1) , D varchar(1))

insert into @Table
select 'X','','',''
union
select 'X','X','',''
union
select 'X','X','X','X'

select * from @Table


Can we access tables into different schema without using the prefix 'schema.' through a db-owner user?

$
0
0

Hi all,

we are trying to access tables into different schema by using the prefix of 'schema.' However would like to know whether there is a possibility of accessing these tables without specifying schema Name. We have four different schemas excluding.dbo


aa


USER DEFINED FUNCTION RETURNING FIRST CHARACTER ONLY

$
0
0

When I execute the function using a SELECT dbo.GETPREVPERIOD statement it only returns the first letter of the value.

The view only contains one record and the PREV_FIN_PER column are defined as varchar 7

CREATE FUNCTION GETPREVperiod()  
RETURNS VARCHAR
AS
BEGIN
DECLARE @PREVPERIOD VARCHAR(15)  

SELECT top 1  @PREVPERIOD = PREV_FIN_PER   
 from  [dbo].[CURR_FIN_YEAR_VIEW]

if (@prevperiod is  null)
BEGIN
RETURN @PREVPERIOD
END
--- Use default workgroup if no match is found
RETURN 'Default'
END

  Please help!!!

Trouble building a dynamic SQL command

$
0
0

i am sort of embarrassed to ask this, should be simple, but i am struggling with this dynamic SQL for the NET USE command.  i just can't get it to work.

Any suggestions are welcomed.

--passed from calling SP

@ZipOUTPath VARCHAR(500)

AS
BEGIN
SET NOCOUNT ON;

DECLARE @MapDrive varchar(500)
SELECT @MapDrive = 'NET USE ' + '"O:"' + '"' + @ZipOUTPath + '"' + Password + /user:Username';
EXEC xp_cmdshell @MapDrive

-- should return O:

EXEC xp_cmdshell 'NET USE';

Help in joining of two tables

$
0
0

Hi, I want to join table #1 and #2 and want to get results from #1 but want to  include all timelines of #1 and #2 in results. Please help on this query. Thanks.

create table #1 (code_p char(4), code_h char(2), code_date datetime, Area char(2), agent char(5))
insert into #1 values ('DEHG','2','1901-01-01','AF','ST')
insert into #1 values ('DEHC','3','1901-01-01','AG','ST')
insert into #1 values ('DEHL','1','1901-01-01','AS','GT')

create table #2 (code_p char(4), code_h char(2), code_date datetime)
insert into #2 values ('DEHG','2','1901-01-01')
insert into #2 values ('DEHG','2','2010-01-01')
insert into #2 values ('DEHC','3','1901-01-01')
insert into #2 values ('DEHC','3','2010-01-01')
insert into #2 values ('DEHC','3','2011-01-01')
insert into #2 values ('DEHL','1','1901-01-01')
/*
Select * from #1 a
join #2 b on a.code_p = b.code_p and a.code_h = b.code_h and

a.code_date = b.code_date

*/
--Expected output
code_p code_h code_date Area agent
DEHG 2  1901-01-01    AF ST
DEHG 2  2010-01-01     AF ST

DEHC 3  1901-01-01    AF ST
DEHC 3  2010-01-01     AF ST
DEHC 3  2011-01-01     AF ST
DEHL 1  1901-01-01    AF ST 

 

fetch language based values in the particular columns

$
0
0

Hi,

I have  a scenario like this. We have a table with values like "Other Language" and "English Language". We need to bring the Other Language values and English Language vaalues in next column.

Table and Value strucre is given below:

DECLARE @TABLE TABLE (SID INT, CONTEXT VARCHAR(100), NOTE VARCHAR(100))
INSERT INTO @TABLE VALUES (1, 'OTHER Language CONTEXT 1','OTEHR Language NOTE 1')
INSERT INTO @TABLE VALUES (2, 'ENGLISH Language CONTEXT 1','ENGLISH Language NOTE 1')
INSERT INTO @TABLE VALUES (1, 'OTHER Language CONTEXT 2','OTEHR Language NOTE 2')
INSERT INTO @TABLE VALUES (2, 'ENGLISH Language CONTEXT 2','ENGLISH Language NOTE 2')
INSERT INTO @TABLE VALUES (1, 'OTHER Language CONTEXT 2','OTEHR Language NOTE 3')
INSERT INTO @TABLE VALUES (2, 'ENGLISH Language CONTEXT 2','ENGLISH Language NOTE 3')
SELECT * FROM @TABLE

and i need a result like as follows:

Other Language Context, Notes and English Language Context, Notes should be side by side

Viewing all 12890 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>