Blocking query in SSMS(SQL Server Management Studio)

I am a Fresher in writing queries.
I wrote following a query to block the A/P invoice, if price added in A/P invoice and the Price in Price list is different.
and this invoice should not be able to add by users other than UserSign = 5,6.
IF (@transaction_type='A' OR @transaction_type = 'U') AND @OBJECT_TYPE='18'
BEGIN
    If EXISTS(SELECT T1.ItemCode,
            T1.Price AS Inv_Price,
            T2.U_ListPrice AS Listed_Price
    FROM OPCH AS T0 
        INNER JOIN
        PCH1 AS T1 ON
         T0.DocEntry = T1.DocEntry
        LEFT OUTER JOIN    
        dbo.[@PRICELISTS] AS T2 ON
         T0.CardCode = T2.U_BPCode AND
         T1.ItemCode = T2.U_ItemNo
    WHERE
        (T0.UserSign <> 6) AND
        (T0.UserSign <> 5) AND
        (T1.DocEntry = @list_of_cols_val_tab_del) AND
        (T1.Price <> T2.U_ListPrice)
        (T2.U_ListCurrency = T0.DocCur)
BEGIN
SET @error = 123 --1234567991234567918
SET @error_message = 'Deviation in price'
END
After executing, still everyone can add the A/P invoice with price difference.
Please look at the above code and help me to find out my fault to make it correct.
Is there any need to add some condition in where clause?
Regards,
Hitul Varia

Hi,
Try this:
IF (@transaction_type='A' OR @transaction_type = 'U') AND @OBJECT_TYPE='18'
BEGIN
    If EXISTS(SELECT T0.docentry
    FROM OPCH  T0 
        INNER JOIN
        PCH1  T1 ON
         T0.DocEntry = T1.DocEntry
        LEFT OUTER JOIN    
        dbo.[@PRICELISTS]  T2 ON
         T0.CardCode = T2.U_BPCode AND
         T1.ItemCode = T2.U_ItemNo
    WHERE
        (T0.UserSign <> 6 OR
        T0.UserSign <> 5) AND
        T0.DocEntry = @list_of_cols_val_tab_del AND
        T1.Price <> T2.U_ListPrice and
        T2.U_ListCurrency = T0.DocCur)
BEGIN
SET @error = 123 --1234567991234567918
SET @error_message = 'Deviation in price'
END
Thanks & Regards,
Nagarajan

Similar Messages

  • Auto Scheduled run of the Sql server management studio or a auto refreshing query

    Hi ALL ,
    This is a very different weird requirement but workaround  for one of my requirement  . Scenario is that sql server enterprise edition is installed on a vm . I create a session with the vm using the remote desktop . I have scheduled a ssis package
    on the server and it should run 4 am daily us timing but its failing . If in case a user is connected to the management studio package runs on time . So i have to disconnect from the remote desktop and its not possible for me to login at 4 am every day for
    running package.
    Requirement :  Is it possible that the sql sever management studio automatically turns on at 03:30 am and authenticates in .If that is possible my package won't fail . If this is not possible  is it possible that sql script auto refreshes every
    30 min's so what i can do i will run the auto refreshing script in the management studio once and will disconnect the remote desktop not the sql server session so my virtually my session on vm will end but vm is always on from the vcenter and there is some
    activity on the sql server management studio so even the application won't be closed after a stipulated time frame leading to running of my package on time.
    Please help

    Hi Latheesh NK ,
    Below is the error message SSIS one i extracted from the history :
    Date        3/8/2014 11:00:00 AM
    Log        Job History (SSIS package)
    Step ID        1
    Server        xyz\xyz
    Job Name        SSIS package
    Step Name        Package
    Duration        00:00:01
    Sql Severity    0
    Sql Message ID    0
    Operator Emailed    
    Operator Net sent    
    Operator Paged    
    Retries Attempted    0
    Message
    Executed as user: xyz\Administrator. Microsoft (R) SQL Server Execute Package Utility  Version 11.0.2100.60 for 32-bit  Copyright (C) Microsoft Corporation. All rights reserved.    Started:  11:00:00 AM  Error: 2014-03-08
    11:00:01.31     Code: 0xC0208449     Source: Data Flow Task 1 Source - Query [95]     Description: ADO NET Source has failed to acquire the connection {68D5B1D7-17B1-4F95-9B1D-7615425C4A07} with the
    following error message: "ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".  End Error  Error: 2014-03-08 11:00:01.31     Code: 0xC0047017     Source:
    Data Flow Task 1 SSIS.Pipeline     Description: Source - Query failed validation and returned error code 0xC0208449.  End Error  Error: 2014-03-08 11:00:01.31     Code: 0xC004700C     Source:
    Data Flow Task 1 SSIS.Pipeline     Description: One or more component failed validation.  End Error  Error: 2014-03-08 11:00:01.31     Code: 0xC0024107     Source: Data Flow Task 1     
    Description: There were errors during task validation.  End Error  DTExec: The package execution returned DTSER_FAILURE (1).  Started:  11:00:00 AM  Finished: 11:00:01 AM  Elapsed:  0.406 seconds.  The package execution
    failed.  The step failed.

  • Can not connect the database via query string (C#). The SQL Server Management Studio is showing the full path of a location instead of database name.

    I can not connect to the database via C#. The database is showing full path of the database file instead of the database name. See the pic: a database showing only name 'emart' and the other database showing the full path.
    : Robby

    Hi,
    According to your post, I know that the database name is showing file path of the database rather than the database name in SQL Server Management Studio. You were not able to
    establish a connection to the database using C#.
    As Olaf said, was any error message thrown out when the connection failed? How did you create these databases?
    You can use the following T-SQL to attach the database and see if the issue persists.
    CREATE DATABASE
    databasename
        ON (FILENAME = 'filepath _Data.mdf'),
        (FILENAME = 'filepath_Log.ldf')
    FOR ATTACH;
    Thanks.
    Tracy Cai
    TechNet Community Support

  • SQL Server Management Studio and Native Client different behaviour on delete

    I have a problem with transaction containing insert and delete to same table and some select/insert/update to some other tables. Problematic table has primary key defined as combination of column1 and column2.
    When two different instances using Native Client execute simultaneously this code and make inserts to table then delete part of code causes deadlock. However this doesn't happen when trying this situation in MS SQL Server Management Studio query.
    Is there some option that is missing from Native Client connection string which can cause this different behaviour?

    Hello,
    I don't think there is a difference in the behavior. SSMS uses ADO.NET and that Provider base on the Native Client.
    The difference will be more that way, when the transaction is commited and so the locks released. I guess your application keeps the transaction (much) longer open; you should commit a transaction as soon as possible to avoid long time locks and so
    deadlocks.
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • The SQL Server Management Studio used for SQL 2012

    Hi,
    Wanted to confirm is SQL Server Management Studio 2008 compatible client for
    SQL server 2012, or does the client come packaged with the SQL 2012

    Related thread:
    http://stackoverflow.com/questions/12136005/sql-server-management-studio-2008-connects-to-sql-2012-localdb
    SQL Server 2012 client tools include SSMS 2012 version.
    Kalman Toth Database & OLAP Architect
    Free T-SQL Scripts
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • Not able to access database from a remote machine using SQL Server Management Studio

    Hi,
    I have a DB_BOX with SQL Server 2008 R2 installed. I can access the databases on the local machine using SQL Server Management Studio but it is not accessible from other machines, though the machines are in same domain.
    I have remote enabled on SQL Server box, TCP enabled, firewall off. I checked with IP Address too, all SQL Server services are running.
    The SQL Server log shows the message
    The requested service has been stopped or disabled and is unavailable at this time. The connection has been closed.
    I get the below message in SSMS from remote machine.
    Details of error message are
    ===================================
    Cannot connect to DB_BOX.
    ===================================
    A connection was successfully established with the server, but then an error occurred during the login process. (provider: TCP Provider, error: 0 - The specified network name is no longer available.) (.Net SqlClient Data Provider)
    For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=64&LinkId=20476
    Server Name: DB_BOX
    Error Number: 64
    Severity: 20
    State: 0
    Program Location:
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
       at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
       at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
       at System.Data.SqlClient.TdsParserStateObject.ReadByte()
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
       at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
       at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
       at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
       at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
       at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
       at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
       at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
       at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.SqlClient.SqlConnection.Open()
       at Microsoft.SqlServer.Management.SqlStudio.Explorer.ObjectExplorerService.ValidateConnection(UIConnectionInfo ci, IServerType server)
       at Microsoft.SqlServer.Management.UI.ConnectionDlg.Connector.ConnectionThreadUser()

    Sorry, missed the message from the errorlog in the original post. You shouldn't have included that big .Net dump that hid the important facts. :-)
    My first Google attempt on that message (which I have never seen before) suggests that the TCP Enpoint is stopped, so try this:
    ALTER ENDPOINT [TSQL Default TCP]
    STATE=STARTED;
    Erland Sommarskog, SQL Server MVP, [email protected]
    This solves the problem. Thanks...

  • Error Message from Microsoft SQL Server Management Studio

    I have problem editing the views that I created.  I never have this problem before and I don't know if
    I have touched something on the pane that alters my viewing on Microsoft SQL Server Management Studio.  For example, if I want to edit the top 200 rows of my view called dbo.sl_admission_reg9, it will give me this error:
    Invalid prefix of suffix characters (MS Viual Database Tools).
    Please help.
    Thank you very much.
    Sally

    Are you trying to edit the top 200 rows of a SQL Server 2012 database using the 2008 R2 SQL Server Management
    Stud? If so you just need to upgrade SQL Server Management Studio to 2012 or edit the rows using a SQL query.
    [Personal Site] [Blog] [Facebook]

  • Impossible to open .dtproje file after re-installing SQL Server Management Studio

    Hi all,
    today, after re-installing SQL Server Management Studio from a package downloaded from MS web site (SQLManagementStudio_x64_FRA.exe), I tried to open a SSIS package file via Visual Studio and I get a message stating that  this type of project (.dtproj)
    is not supported.
    Here are the events that lead to this problem:
    We have SQL Server 2005 installed on a server and are planning to migrate to SQL Server 2012.
    1 - Earlier this year, to solve a problem that we had with the SSMS client, we installed SSMS 2012 client on my station.
    At this point, it worked fine for SSMS and when I tried to access SSIS it converted the packages  to SSIS 2012 and it worked fine too.
    2 - Yesterday, I tried to access SSMS and I had the message that the test period had expired; at this point I could open a SSIS file.
    3 - To solve the problem with SSMS we decided to re-install it. It worked fine for SSMS but now I am not able to open a SSIS file.
    Do you have any idea of what the problem is and what should I do to solve it?
    It seems that there are some SSIS component missing, how could I get them back?

    You are welcome Sylviep,
    Based on what I see you want to create BI projects (e.g. a SSIS project), thus it will be enough to install SSDT
    which is part of the SQL Server installation media. I do not see why you would re-install SQL Server itself.
    Arthur My Blog

  • What version of SQL Server Management Studio do I need?

    I need to install SQL Server Management Studio on my windows 8.1 PRO 64-bit pc to manage SQL server 2008 R2. do I download the 2014 or 2012  or 2008 version?

    Hello,
    Install the SQL Server Management Studio (SSMS) of the same version of SQL Server you have installed.
    If you modify maintenance plans using a newer version of SSMS, you then cannot use a lower version of SSMS to modify/change it again.
    Hope this helps.
    Regards,
    Alberto Morillo
    SQLCoffee.com

  • Can I use Microsoft SQL Server Management Studio version 11.0 to write SQL queries for "SQL Server Compact 4.0 Local Database"

    Hi, Can I use Microsoft SQL Server Management Studio version 11.0 to write SQL queries for "SQL Server Compact 4.0 Local Database" ?
    When I use Connect Object Explorer, the "Connect to Server" dialog box which pops up has only 4 selections in the Server Type Drop Down List. They are Database Engine, Analysis Services, Reporting Services & Integration Services. I have read
    somewhere that there should be a compact database option. but I do not see it.
    What I would like to do is use free form SQL Queries against the tables in "SQL Server Compact 4.0 Local Database" .
    Once I have validated these queries, then I will use them in my Visual Studio 2012 C#, ASP.NET application. I created the Local Database using Visual Studio 2012 for use by my application.
    Thank you for your help..
    diana4

    Hello,
    With SSMS 2005 we have had the Option to work with SQL CE database files, but not with higher Version of SSMS.
    You can use the free SQL CE Toolbax instead; see
    http://sqlcetoolbox.codeplex.com/
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • How to register VSPackage into SQL Server Management Studio?

    Hi All,
    I am a newer to VSPackage, I want to create a VSPackage for SQL Server Management Studio(SSMS), now I can create a VSPackage project and debug it in Visual Studio but don't know how to debug it in SQL Server Management Studio and how to deploy VSPackage to
    SSMS by MSI? Thanks

    By the way I find a extension.vsixmanifest file and some .pkgdef files under
    C:\Program Files\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Extensions\  folder, and they are very similar with VSPackage files. Do know this is useful or not.
    Hi Vic Zhang,
    After some research, There are some notes. SQL Server Management Studio is built upon the Visual Studio Isolated Shell, which inherently supports extensibility (add-ins/plug-ins). It is possible to tap into the Visual Studio extensibility services to surface
    custom capabilities within SQL Server Management Studio; however, we do not discourage this, keep in mind that such extensibility is not supported, so there may be issues with backward/forward compatibility. Microsoft does not publish documentation for extending
    SQL Server Management Studio.
    If you want to debug VSPackeage about SSMS, I recommend you debug in Visual Studio and not in SSMS, open the project and attach to the process of SSMS.exe, then debug it.
    About the issue of finding the “RootKey”, a .pkgdef file is way to encapsulate application configuration information in an easily editable, distributable, and deployable form. And it registers the Extension Manager (which is written as a VS Package). If
    you install SQL Server 2012, the .pkgdef of SSMS is located in the following path, you can check the “RootKey” in this .pkgdef file.
    C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio
    There is detail about a PkgDef, you can review the following article.
    http://blogs.msdn.com/b/visualstudio/archive/2009/12/18/what-s-a-pkgdef-and-why.aspx
    Regards,
    Sofiya Li
    Sofiya Li
    TechNet Community Support

  • How to find a record then edit using sql server management studio

    Hello,
    A very basic question:
    I want to find a record stored in MS SQL Server 2008 express database table then edit the record.
    I tried to do it using SQL Server 2008 management studio express because I have been using MS Access database all the time. I have right clicked the table in management studio, clicked the "Edit top 200 records" menu item then when records were
    displayed on the result pane, I pressed Ctrl + F key to find a student named Simon for example. Well, nothing happened. I couldn't see any dialog boxes for putting in the search criteria.
    I guess I cannot simply use Ctrl+F in SQL Server management studio to find / edit a record.
    Can someone please teach me how to find a record specifying a search criteria then edit the record in SQL server management studio?

    Hi ZKM128,
    I have not seen Ctrl+F to be used to look for desired rows in Sql server.
    On the result pane after you selected "Edit top 200 records", just right click , select
    PANE, SQL, you will get the T-Sql query t,here you need to mention the exact criteria which you are looking for, and then you will get the desired rows and then you can edit.
    Thanks
    Manish
    Please use Marked as Answer if my post solved your problem and use
    Vote As Helpful if a post was useful.

  • Load grid like SQL Server Management Studio

    Hi
    we have a windows forms project with grid.
    I would like to emulate the behaviour of SQL Server management studio where upon executing the query, the results start filling in instantaneously and the process continues until all results have been fetched. This way the user doesnt have to wait at all.
    Kindly advise whats the best way to achieve above.
    Many Thanks
    Abhishek

    In a virtual mode datagridview, you should only read data when necessary. The whole point of generating extra DataGridView.CellValueNeeded events is to give you the option of not pulling all rows, only for currently visible ones (maybe a little more
    for caching). 
    You should at least cache the data of a row when a cell is accessed (it is likely you will query every column of a row). It also a good idea to cache several pages near the requested row, since the user may be scrolling up and down a bit and you may not
    want to hit the database for every single row or every even single page. The few pages near the top and the bottom are also hot areas that are good for caching.  BackgroundWorker is kinda clumsy for this task, probably better write this in TPL and queue
    data populating tasks when the user scroll up or down.
    The caching part is your business logic and is not provided by Windows Forms.
    Visual C++ MVP

  • SQL Server Management Studio File Open dialog is very slow to change directories

    I am running Microsoft SQL Server Management Studio    10.0.2531.0 on Windows 7 Professional.
    I have an intermittent problem with the File Open dialog. Sometimes it works reasonably well, but often it takes a very long time (a few minutes) to change directory. While it is doing whatever it is doing, the dialog and all of SSMS becomes unresponsive
    - I can't even cancel the dialog. It sometimes changes the cursor to an hourglass and sometimes not.
    I do not have this problem with any other application.
    I can open File Open one time and have this delay for every single directory change or if I open the Look In dropdown then, after closing it I can open it again and it will work with normal delays.
    It is slow more often than it is normal.
    It makes accessing files very impractical.

    Hi,
    10.0.2531.0 is SQL Server 2008 Service Pack 1 (release April 2009)
    No guarantee this will fix your issue, but may be worth testing the latest Service Pack 3 (released October 2011) to see if that helps
    http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27594

  • MS SQL Server Management studio error

    I am using MS SQL Server Management studio to query a sql db and unable to connect using the Windows Authentication and get the following error message.  Can someone help me troubleshoot this please?
    Login failed for user 'domain\windows username', (Microsoft SQL Server, Error: 18456).
    Regards

    This error occurs when user won't have access on SQL server. Refer below to article to resolve the issue:
    http://support.microsoft.com/kb/555332/en-us
    Please click the Mark as answer button and vote as helpful if this reply solves your problem

Maybe you are looking for

  • How would I writ a file to find a playlist named PL-Mix 01??

    Ever since "itunes" quit making available the printing of CD jewel case inserts with their fabulous update of "itunes 11"  I have been looking for other ways to print my song titles and artist for my CD's. I know alot of people are using ipads and ip

  • Print prview for goods issue

    hi all, how can i see the print preview for goods issue? regards, satheesh

  • Backup hangs using Desktop Manager 5.0 (and 4.7)

    Hello, I have a Blackberry 9630 (on Telus) running OS 4.7.1.40. Whenever I try to backup my phone using DM 5.0.0.11 (as well as with the previous version 4.7xx) I get a window that reads "Transfer in progress" but nothing happens -- the window just h

  • Prores 422 clips not working

    I cant import prores 422 clips into my editing software, or play them in quicktime error message for final cut : 'file error: 1 files(s) recognized, 0 access denied, 1 unknown' error message for after fx: 'after effects error: file 'filename.mov' can

  • Password reset - over and over and over

    I have reset my password no less than 5 times this evening. Every time I reset it, I can not log into skype. I can log into Live.com once and then even that says I have entered the wrong password. I created a new account and got logged in. Then I wen