mercredi 30 septembre 2015

SSMS 2014 - Cannot insert multiple values into table variable

This is probably something really trivial I'm missing, but I can't seem to figure out why it's not working:

Basically, this works:

DECLARE @names TABLE (name NVARCHAR(100))
;
INSERT INTO @names
VALUES
    ('John')
;

but this does not:

DECLARE @names TABLE (name NVARCHAR(100))
;
INSERT INTO @names
VALUES
    ('John'),
    ('Jane')
;

I'm getting this error:

Msg 102, Level 15, State 1, Line 5

Incorrect syntax near ','.

Why wouldn't this work? I've done this thousands of times with SSMS 2008.

Sql query with join to return a part number that exists in one table and does not exist in another table with other field data

I'm trying to return data from one table A that does not exist in table B with certain data. For instance:

Table A     No_         Inventory Posting Group
             1           PART
             2           NEW
             3           PART

Table B     No_         Table ID       Dimension Code
             1           27             Branch
             2           27             Manufacturer
             3           27             Department

Every part No_ exists in table A. Table B needs to have both "Branch" and "Department" on a separate row. Some parts have one or the other or both in Table B. I'm trying to return results that show for every part listed in table A which ones are not present for "Branch" and "Department" in table B. It may be two separate queries. Here is what I have now, but I'm not getting what I want.

select dd.[No_], dd.[Table ID], dd.[Dimension Code], dd.[Dimension Value Code], dd.[Value Posting], dd.[Multi Selection Action]
from [Item] i left outer join [Default Dimension] dd

on  i.[No_] = dd.[No_]

where dd.[Dimension Code] not in ('BRANCH', 'DEPARTMENT')
and i.[Inventory Posting Group] = 'PAR'
and dd.[Table ID] = 27

How to get Sql Record with out any reference on given example

I have two table as table1 and table 2 as enter image description here

I want one table3 structure as

enter image description here

I dont have any of the reference between upper two tables.

1.Is it possible in single select statement?

Or

2.Required looping for that.

What I have tried is

    select E.EmpID,E.FName,E.LName,C.CityName,E.Salary,DOJ,
    case
    when E.Salary > 0 and E.Salary < 30001 then 'Trainee'
    when E.Salary > 30001 and E.Salary < 60001 then 'Jr. Developer'
    when E.Salary > 60001 and E.Salary < 150001 then 'Sr. Developer'
    when E.Salary > 150001 and E.Salary < 180001 then 'Project Lead'
    when E.Salary > 180001 and E.Salary < 250001 then 'Project Manager'
    else '' end  as Designation 
    from Emp_Master as E inner join City_Master as C
    on E.CityID = C.CityId 

which I think is totally wrong

Please answer me Providing example

Can we drop and recreate primary key in SQL Server table @ Production environment? Do we have to down the server for it or we can do it live?

Can we drop and recreate a composite primary key in a SQL Server table in production environment? Do we have take down the server for it, or can we do it live?

Because we have to add more columns to the primary key. If we done it in live what are the problems we have to face?

mardi 29 septembre 2015

ignore bcp right truncation

I have a file with the stock information, such as ticker and stock price. The file was loaded to database table using freebcp. The stock price format in the file is like: 23.125. The stock price data type in database table is [decimal](28, 2). freebcp loaded the data to the table without any problem by ignoring the last digit: 23.12 was loaded to the table column of the record. We are now using Microsoft SQL Server's bcp utility (Version: 11.0 ) to load the data. However we now encounter an issue: bcp considers loading 23.125 to decimal(28.2) is an error (#@ Row 783, Column 23: String data, right truncation @#). It rejected the record.

I didn't want to modify the input file, because there are a lot of columns in the file need to be fixed by removing the last digit of columns.

Are there any ways to construct the BCP or the Microsoft SQL Server to ingore the right truncation error?

Thanks.

Charlie

lundi 28 septembre 2015

Trying to create a trigger in SQL server that converts and inserts an xml string

Have created this trigger

It completes successfully and enters into the right tables, however when it all processes through on the table that it inserts into the table the Long Message section of the trigger has "NULL" inside in there.

It is not often that I look at SQL really. The actual part of the script that converts the data to XML string on its own works and makes a string. However when it is in the trigger it doesn't populate anything.

///////////////////////////////////////////

USE [SysproCompanyP]
GO
/****** Object:  Trigger [dbo].[trig_DNIP]    Script Date: 28/09/2015 11:25:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


--=======================================
-- Author       : Thomas Mullins
-- Create date  : 02/09/2015
-- Description  : Email event on a dispatch note being printed
-- Update
-- Update
--=======================================

ALTER TRIGGER [dbo].[trig_DNIP]
      ON [dbo].[MdnMaster]
      AFTER INSERT
AS 
BEGIN 

DECLARE @ConstCompany AS CHAR(1)
DECLARE @SourceCompanyFull AS CHAR(40)

DECLARE @DispatchNote AS CHAR(15)
DECLARE @SalesOrder AS CHAR(6)
DECLARE @CustomerName AS CHAR(30)
DECLARE @Customer AS CHAR(7)
DECLARE @var_LongMessage AS CHAR (255)



SET @SourceCompanyFull = DB_NAME()
SET @ConstCompany = UPPER(RIGHT(RTRIM(@SourceCompanyFull),1))

IF @ConstCompany = 'S'
BEGIN 
   SET @ConstCompany = '0'
END


SELECT 
    @DispatchNote = RTRIM(DispatchNote), 
    @SalesOrder = RTRIM(SalesOrder),
    @CustomerName = RTRIM(CustomerName), 
    @Customer = RTRIM(Customer)
FROM INSERTED 


SET NOCOUNT ON; 

IF @Customer in ('CN01PIO', 'CN01WUH') --AND datepart(yyyy, @CreationDate) = datepart(yyyy, getdate() and datepart(mm, @CreationDate = datepart(mm, @CreationDate) = datepart (mm,getdate()) and datepart(dd, @CreationDate) = datepart(dd, getdate()
BEGIN 

    SET @var_LongMessage = (SELECT (STUFF((
               SELECT ',  ' + RTRIM(WRL.StockCode) + ' ' + RTRIM(WRL.Job) + '     '
               FROM SysproCompanyP.dbo.MdnDetailLot (nolock) AS MDL 
               LEFT OUTER JOIN SysproCompanyP.dbo.WipReservedLots (nolock) AS WRL on WRL.Lot = MDL.Lot 
               WHERE MDL.DispatchNote = RTRIM(@DispatchNote)
               GROUP BY WRL.StockCode, WRL.Job 
               FOR XML PATH('')
               ), 1, 2, '')
            ))

INSERT INTO [SysproCompanyC].[dbo].[EmailEvents]
       ([SourceCompany]         ,[SourceCompanyFull]    ,[ParentValue]          ,[CFFName]          ,[CFFData]  ,[TxtFileExtension]  ,[DateEntered]    ,[LongMessage])
VALUES 
       (@ConstCompany          ,@SourceCompanyFull    ,@SalesOrder            ,@Customer      ,@DispatchNote    ,'DNIP'                ,GETDATE()       ,@var_LongMessage)

INSERT INTO [Piolax_Logging].[dbo].[EventLogging]   ([Company],[EventType],[Source],[Annotation],[NumberOfRecords],[DateTime])
        VALUES (@ConstCompany           ,'INFO'           ,'[trig_DNIP]'           ,'Dispatch Note Printed  '  + @DispatchNote + @ConstCompany + @SourceCompanyFull + @SalesOrder  + @Customer + 'DNIP' + HOST_NAME(),1           ,GETDATE())
END 




END

If anybody could give my any info that would be great, thanks.

vendredi 25 septembre 2015

unable to connect SQL server 2005 VC++ using ATL and window auth

I can connect to DB without using winAuth, but not ok if I using winAuth.

Here is the permission setting, I am using EFTG\eftg_app_svc

I cannot post image, but I believe I have correct permission setting.

And configured the SQL server can use window auth.

And here is my code:

HRESULT OpenDataSource()
    {
        HRESULT     hr;
        CDataSource db;
        CDBPropSet  dbinit(DBPROPSET_DBINIT);
    if (m_DBCfg.GetWindowsAuthFlag())
    {
        dbinit.AddProperty(DBPROP_AUTH_INTEGRATED, OLESTR("SSPI"));
    }
    else
    {
        dbinit.AddProperty(DBPROP_AUTH_PASSWORD, OLESTR(""));
        dbinit.AddProperty(DBPROP_AUTH_USERID, m_DBCfg.GetDatabaseUsername());
    }
    dbinit.AddProperty(DBPROP_INIT_CATALOG, m_DBCfg.GetDatabaseName());
    dbinit.AddProperty(DBPROP_INIT_DATASOURCE, m_DBCfg.GetDatabaseServer());
    dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033);
    dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
    dbinit.AddProperty(DBPROP_INIT_OLEDBSERVICES, DBPROPVAL_OS_RESOURCEPOOLING);
    hr = db.OpenWithServiceComponents(_T("SQLOLEDB.1"), &dbinit);
    if (FAILED(hr))
        return hr;

But DB eventlog: -> Login failed for user 'EFTG\eftg_app_svc'. [CLIENT: ] What's wrong???

Thanks very much!!